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

Illuminate\Database\Eloquent\Model will not boot properly in a second app #4036

Closed
tomzx opened this issue Apr 4, 2014 · 2 comments
Closed

Comments

@tomzx
Copy link
Contributor

tomzx commented Apr 4, 2014

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 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.

@taylorotwell
Copy link
Member

Can you call the boot method manually in your setUp method?

@tomzx
Copy link
Contributor Author

tomzx commented Apr 4, 2014

Hi @taylorotwell,

Is this the recommended way of doing this?

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.

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

2 participants