Using factories in Unit-tests don't work in a new laravel project. Perhaps it is intended, but then it should be in the documentation. And I believe I have used factories in Unittests before...
public function testBasicTest()
{
factory('App\User')->make();
$this->assertTrue(true);
}
Now the tests throws an error, saying:
InvalidArgumentException: Unable to locate factory with name [default] [App\User].
As expected it does not make a difference if using the User::class Property as param
Using the same code in featuretests works as expected.
factories definitely work in Unit Tests, so my guess is this is a problem specific to your project, and not a Laravel bug.
I would recommend closing this issue and moving the conversation over to the appropriate forums to get quicker help.
Laravel Slack (https://larachat.co/)
Laravel.io Forum (https://laravel.io/forum)
Laracasts Forum (https://laracasts.com/discuss)
StackOverflow (http://stackoverflow.com/questions/tagged/laravel)
Thanks!
@browner12 : then it is propably PHP, Windows or PHPUnit.
As you can see in the steps to reproduce it is a completly fresh laravel! All other files are unchanged (even the .env, phpunit.xml and the config-files). Just the one line with the factory was added. And strangely the same code works in the FeatureTests...
Perhaps i will find some time on Sunday to test it on a linux vm or the rasperry.
Additonal Info on my OS, Win 10 Pro, 1909, 18363.535 - but I don't believe that this is not the problem here
I still don't know as I would rate this as a bug as it's just a design decision but in 6.8 the example unit test (and the test stubs generated by php artisan make:test --unit) are now extending from the PHPUnit test case instead of the TestCase class defined in the tests folder. In a practical sense, this means you app is not being booted and therefore, yes, the factories defined in database/factories are not loaded. If you need to use parts of the laravel application for your unit tests, just change the unit test class to extend from the TestCase in that folder. Look to the example feature test, which is still extending from that TestCase, to see how to do that.
good catch @alec-joy, I forgot that switch was recently made to the tests.
Unit tests were recently updated to make use of the PHPUnit TestCase class so the framework isn't booted (hence why factories aren't available). This was done because in general, you want your unit tests to run as fast as possible. Objects in your unit tests are usually tested in isolation without any outside dependencies.
If you need a factory consider placing your test in the Feature directory.
Thanks to all answers, and a question.
Would be there any chance to add 2, 3 sentences in the documentation about this situation? The design decision is clear and convincing, but not obvious.
If wished I can try to do this, but someone should correct my best English into a more useful one.
Most helpful comment
Thanks to all answers, and a question.
Would be there any chance to add 2, 3 sentences in the documentation about this situation? The design decision is clear and convincing, but not obvious.
If wished I can try to do this, but someone should correct my best English into a more useful one.