Eloquent model events only trigger once in tests
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
An issue popped up on the laravel/framework repository a while ago that identified an issue where model events would only fire once when being tested, despite the fact they would fire and work as expected in production. This was particularly interesting to me because it was a problem for anyone who uses my watson/validating and tests their apps as well. Because the library leverages Eloquent's events to perform validation it becomes very difficult to test that your models are working as expected.
You can see issue #1181 here.
However, there is an (unpleasant) solution. Hopefully this gets patched by the framework, otherwise here is the workaround. Basically, you need to tell each model to flush it's event listeners and then re-register them between tests.
You can see that this could become painful quickly, especially if you have a lot of models. Let's improve, in your TestCase.php
file:
Oh, you still have to pop your models in the array, you say? How annoying! Also an easy fix, as long as you keep your models all in a single directory.
Now you can simply use $models = $this->getModels()
to automatically load all the model names. I expected some reduced perfomance with this one but my tests actually improved by 2 seconds. Go figure.
This fix is also available complete as a gist.