-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Factories don't work in _unit_tests #30879
Comments
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/) Thanks! |
@browner12 : then it is propably PHP, Windows or PHPUnit. Perhaps i will find some time on Sunday to test it on a linux vm or the rasperry. |
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 |
Thanks to all answers, and a question. If wished I can try to do this, but someone should correct my best English into a more useful one. |
Description:
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...
Steps To Reproduce:
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.
The text was updated successfully, but these errors were encountered: