-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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 created in setUp are not removed by the database transaction #9230
Comments
Just a side note: @dwightwatson , you are not using references, are you? You are manually linking posts to users here. At least I don't think this is correct usage of references with ORM (it will work in SQL-side of course, but ORM generally differs references from generic value assignments). And if you noticed, the stuff you did in actual test method (adding posts) was reverted after it finished. Everything else outside that method's scope remained (creating User model). When you move User models into the actual method, then it gets reverted as well. Looks to me like PHPUnit related case, not Laravel one. |
Yeah, I am manually linking the posts to the user. I suppose I could have linked the models using the relationship, but didn't in the example above. It wouldn't make an actual difference in practice, apart from having the attribute fillable.
You could be right, this might be a PHPUnit issue. I don't think it's expected behaviour, the It's annoying because this can easily result in the duplication of testing code, or one having to make their own set up method that would need to called manually before each test. I might leave this issue open a little longer as I'd be interested in seeing what other people think about this, whether there is possibly a solution for Laravel or I'm just being pedantic. |
Yea, but as I was taught recently in #8459:
Which technically means tons of rewrites and dupes. For that exact reason, during code quality analysis, certain folders, including |
This is happening because your There's a PR to get this fixed in PHPUnit 4.5. |
Thanks @JeffreyWay. Closing this issue as this is an issue with PHPUnit. |
If you have a test case that needs a certain model for every individual test the setup logic is usually places within the
setUp()
method. However, any models that are created through a factory in thesetUp()
method are not then removed following each test.Following this test the 3
Post
models will be removed from the database, however theUser
model remains. I don't think this is the expected behaviour and can muck up your test suite pretty easily.If the code from the setup component was moved into each test then the
User
model is removed as expected.The text was updated successfully, but these errors were encountered: