rawilk's avatar
Level 47

Test Laravel console command from package

I developed my own version of a package and it has console commands that I need to test. All the tests run fine except for the ones that assert an output from artisan. For some reason, Artisan::output() returns "" when running the tests.

Could it be that I don't have an application instance in the tests like a normal Laravel app that extends Illuminate\Foundation\Testing\TestCase instead of mine that extends Orchestra\Testbench\TestCase? If so, is there any way to replicate that for a package because I would really get these last few tests to pass.

An example of one that is failing can be found here: https://github.com/rawilk/laravel-modules/blob/master/tests/Commands/ModelMakeCommandTest.php#L81

I know the output is there because I ran the command like I have in the test in an application that is using the package and it displays the expected output in the console. I just really want to get the test to pass.

0 likes
4 replies
rawilk's avatar
rawilk
OP
Best Answer
Level 47

Of course after asking the question I found how to solve it. I didn't realize that the console gets mocked now and you can prevent that behavior in your tests. Adding the following line in the setUp function in my base test class fixed my issue:

public function setUp()
{
    parent::setUp();

    // This fixed the issue
    $this->withoutMockingConsoleOutput();
}
1 like
rawilk's avatar
Level 47

Also, I hate to be "that guy" that marks their own reply as the best reply, but I did find a legitimate solution to my question.

rawilk's avatar
Level 47

Thanks @Cronix, I did try that as well but it was the same problem with nothing being returned from the output. Turns out it was just being mocked and I had to disable that with withoutMockingConsoleOutput.

Please or to participate in this conversation.