Courses

Laravel API Code Review and Refactor

Feature/Unit Tests: Make them Work

You're reading a FREE PREVIEW of a PREMIUM course.

Link to the repository

[Only for premium members]

The next thing I usually check is whether the project has tests. It means I can try to modify the code and run the tests to check if I didn't break anything.

And we do have some tests here:

But when I try to run them, I get an error:


Configuring Unit/Feature Tests

Well, yeah, I don't see the tests/Unit folder. And don't get me wrong: it's totally fine NOT to have Unit tests, staying only with Feature tests. But the default Laravel comes with the example Unit test:

tests/Unit/ExampleTest.php:

namespace Tests\Unit;
 
use PHPUnit\Framework\TestCase;
 
class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}

In this case, the code author has deleted that example test with the full folder of tests/Unit. It may be fine, but the problem is that testing is still configured to run tests from the tests/Unit folder, which doesn't exist anymore:

phpunit.xml

<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>

So, we have two choices here:

  • Either create a tests/Unit empty folder
  • Or, specify in...

The full lesson is only for Premium Members.
Want to access all 15 lessons of this course? (56 min read)

You also get:

  • 76 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord