[9.x] Enable trait setUp/tearDown for tests traits. #39287
Replies: 1 comment
-
I believe you already PR this and merged. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When dealing this testing, sometimes you may want to share initialization between many Test Classes. For example, you may want to set up a Post with an User and three comments.
Doing this across many Tests becomes cumbersome, specially if you need to mix additional setUp and tearDown procedures.
Instead, we can use the same technique of trait initialization and booting in Models, but in tests.
Implementation
On the
setUpTraits()
method, we can setup the trait savingsetUp{trait}
andtearDown{trait}
into a callback that later can be called by the application.This would mean that traits like
RefreshDatabase
,DatabaseMigrations
,DatabaseTransactions
,WithoutMiddleware
,WithoutEvents
andWithFaker
would change their booting methods, making it a breaking change for Laravel 9.x.Example
With the above, we can create a trait that creates a Post with an User and 3 Comments, keeping the implementation clean in the Test Class:
Beta Was this translation helpful? Give feedback.
All reactions