PepsiIsBetter's avatar

Using helper function on unit testing by replacing TestCase

Hi,

The helper couldn't work on unit test if I created the unit test via php artisan make:test UserTest --unit

To solve this, I need to manually replace use PHPUnit\Framework\TestCase; to use Tests\TestCase;

But it is weird to do that, the php artisan command is supposed to do all the work, right?

And, when I use Tests\TestCase instead of PHPUnit\Framework\TestCase, would I miss anything that PHPUnit provide for unit testing?

Any idea?

0 likes
3 replies
mabdullahsari's avatar
Level 16

No, artisan is not supposed to do the heavy lifting. It is however supposed to scaffold a base test case which you can extend.

Your helper is not working most likely because it is interacting with the IoC container. If you take a closer look inside of the "Bare" TestCase Laravel provides out of the box, you can see that it creates an application instance each time a test class is run.

You won't "lose" any functionality because if you take a look at 2 classes deeper:

use PHPUnit\Framework\TestCase as BaseTestCase;

It is being extended by Tests\TestCase anyway, ie. Laravel's test suite is based upon PHPUnit.

You would however miss very handy assertion methods the Illuminate\Foundation\Testing\TestCase class provides if you'd not extend that as your base test case.

1 like
PepsiIsBetter's avatar

Thank you so much for reply, I'll take a look at the TestCase.

PepsiIsBetter's avatar

I found a PR of unit test of Laravel which change use Tests\TestCase to use PHPUnit\Framework\TestCase, the description gives many detail and help me more understand of testing.

(Although I still need to use Tests\TestCase on my unit test, because there is a helper in my method)

https://github.com/laravel/laravel/pull/5169

Please or to participate in this conversation.