Codeception / AssertThrows

Assert exception handling without stopping a test. For PHPUnit 6+
MIT License
19 stars 10 forks source link

Tests not passing with Green when throwing exceptions #17

Closed theMyth721 closed 2 years ago

theMyth721 commented 2 years ago

This isn't working properly. I added it as a trait. But it made no difference. Although the error is not being thrown, it is still showing the exception in the console and not showing the exception.

I doubt I will be using PHP much more, but I am building this last project in it and I would like for it to work

/**
 * Runs test with a Prefix, an optional Class Level Path
 */
class RouteMatcherPrefixTest extends \Codeception\Test\Unit
{

    /** Use this trait to test Exceptions */
    use \Codeception\AssertThrows;

    /**
     * @var \UnitTester
     */
    protected $tester;
/**
     * Given, a wild card path, no matter what is put after it
     * When request for matching class path is called,
     * It returns true
     */
    public function testWildCardReturnsTrueNoMatterWhatAfter()
    {
        $this->assertThrows(RoutingException::class, function () {
            $result = $this->rm->matchClassPath("/*/fdfgfg/fgfgf/fgfgf");
            $this->assertFalse($result['exact']);
        });

    }`
Naktibalda commented 2 years ago

Could you show what error output you got?

theMyth721 commented 2 years ago

It just throws the Exception on the Console directly and I don't get the green colours stating the test passed. I will send this shortly today. I am building this framework for a university project and is probably the last time I will code in PHP but this is the only dependency I am using

theMyth721 commented 2 years ago

So when the wrong exception is thrown the test fails properly as expected:

Screenshot 2022-05-17 at 11 39 49

But when this error is fixed and I call the public method, then it gives this error:

Screenshot 2022-05-17 at 11 40 57

So the Exception is being thrown perfectly, I have an echo method in the output that is outputting everything to the screen.

I dont really want that, I want to be able to see the test pass.

When I scroll up, It doesn't say the test failed, which is good but it doesn't show the test passed:

Screenshot 2022-05-17 at 11 42 01

Here is the code:


public function setConnection($sqlConfig)
    {
        try {

            if (!array_key_exists("connection", $sqlConfig) &&  !array_key_exists("connections", $sqlConfig)) {
                throw new DatabaseConnectionException("Database Connection Exception! Either a connection array or a connections array is required!");
            }

            if (isset($sqlConfig['connections'])) {

                if (empty(array_keys($sqlConfig['connections'], "default"))) {
                    throw new DatabaseConnectionException("Database Connection Exception! Default database must be specified with multiple connections!");
                }

                foreach ($sqlConfig['connections'] as $connectionName => $connection) {
                    //
                }
            }

        } catch (DatabaseConnectionException $th) {
            $th->printStackTrace();
            throw $th;
        }
    }
`public function testThrowsExceptionWhenNoConnectionOptPresent()
    {
        $this->assertThrows(DatabaseConnectionException::class, function(){
            $this->dbManager->setConnection([
                'abc' => 'blah',
                'booboo' => 234
            ]);
        });

    }`
theMyth721 commented 2 years ago

I am not sure if it is an error or not. The exceptions are thrown. But the test passed is not showing, instead it just outputs all the html.

I do need to separate that from the output that should go in the terminal

Naktibalda commented 2 years ago

Is that line with unfinished test the last line of output you get from Codeception? What version of Codeception do you use?

theMyth721 commented 2 years ago

Hi, Thank you for getting back to me. The last line I get is COMMAND DID NOT FINISH PROPERLY

Screenshot 2022-05-17 at 13 52 17
Naktibalda commented 2 years ago

That's what I expected to see.

Your application prints that HTML and then calls exit or die. These functions make your code untestable.

theMyth721 commented 2 years ago

Oh okay, so If I remove all that code, then I will get the green test passed? Thank you for your input, Are you suggesting I should not have die after? Or just not print HTML? I can configure it in some way to not print html in the console, but print in the browser

Thanks, I figured it out, I shouldn't really be doing the die.

Naktibalda commented 2 years ago

We are talking about unit test of SqlDbManager here. It certainly shouldn't print any HTML or call die.

theMyth721 commented 2 years ago

Thanks, That helps a lot. I did what you suggested and it works fine now. I am building a framework for a university project & Codeception is the only dependency I used. I just want to be able to create a little CLI script that will automatically produce a unit, integrated tests when I create a new service / entity etc.. But im working on that now. It was my error, sorry about that

Naktibalda commented 2 years ago

no problem :)