Skip to content
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

Closed
EmmaPurple opened this issue Dec 18, 2019 · 6 comments
Closed

Factories don't work in _unit_tests #30879

EmmaPurple opened this issue Dec 18, 2019 · 6 comments

Comments

@EmmaPurple
Copy link
Contributor

EmmaPurple commented Dec 18, 2019

  • Laravel Version: 6.8.0
  • PHP Version: 7.3.9
  • PHPUnit Version: 8.5.0
  • Database Driver & Version: MariaDb 10.4.6

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:

  1. Create a new laravel project
  2. Change the 'tests > Unit > ExampleTest > testExample' to
    public function testBasicTest()
    {
        factory('App\User')->make();
        $this->assertTrue(true);
    }
  1. Run the Test

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.

@browner12
Copy link
Contributor

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!

@EmmaPurple
Copy link
Contributor Author

@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

@alec-joy
Copy link

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.

@browner12
Copy link
Contributor

good catch @alec-joy, I forgot that switch was recently made to the tests.

@driesvints
Copy link
Member

driesvints commented Dec 19, 2019

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.

@EmmaPurple
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants