You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently having issues in my test cases where models are not booting properly.
In Illuminate\Foundation\Testing\TestCase::refreshApplication(), a new application is created on every test setUp(). This is great for test isolation, but there is an issue where previously booted models will not have their event bindings setup properly for any subsequent tests.
What happens is that the event bindings are setup on the first test, bound to the first app dispatcher instance and upon the second test setUp, the dispatcher used to fire events is a different one (since a new app was created with its own Dispatcher).
Meanwhile, in Illuminate\Database\Eloquent\Model::bootIfNotBooted() the event bindings that would be added in the Model's boot method are not added (to this new dispatcher) since the Model exists in Illuminate\Database\Eloquent\Model::$booted and is thus considered "booted" (which is true for the initial dispatcher, but not the new one).
A simple repro case would be a test class with 2 test methods and a model which registers events in its boot method.
I hope this makes sense, let me know if anything needs clarification.
The text was updated successfully, but these errors were encountered:
If I do so, then the boot method ends up being called twice (for instance, MyModel::boot() and then during the bootIfNotBooted() constructor call) and instead you have the events bound twice during the first test. Subsequent tests are fine then.
Hi,
I'm currently having issues in my test cases where models are not booting properly.
In
Illuminate\Foundation\Testing\TestCase::refreshApplication()
, a new application is created on every testsetUp()
. This is great for test isolation, but there is an issue where previously booted models will not have their event bindings setup properly for any subsequent tests.What happens is that the event bindings are setup on the first test, bound to the first app dispatcher instance and upon the second test setUp, the dispatcher used to fire events is a different one (since a new app was created with its own Dispatcher).
Meanwhile, in
Illuminate\Database\Eloquent\Model::bootIfNotBooted()
the event bindings that would be added in the Model'sboot
method are not added (to this new dispatcher) since the Model exists inIlluminate\Database\Eloquent\Model::$booted
and is thus considered "booted" (which is true for the initial dispatcher, but not the new one).A simple repro case would be a test class with 2 test methods and a model which registers events in its boot method.
I hope this makes sense, let me know if anything needs clarification.
The text was updated successfully, but these errors were encountered: