Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing before_filter gets called in action

How would one test that when an action in controller is being accessed, that controller's before_filter will be executed?

The before_filter is tested in separate example group so that there's no need to duplicate the tests for all actions that depend on the before_filter.

If I have

controller.should_receive(:my_before_filter)

in my example, it works fine. However, having the above expectation seems to cause that the logic in my_before_filter is not being executed (it should assign an instance variable).

How to overcome this restriction or to mock behaviour of my_before_filter (it sets instance variable on controller)? Or is there some better way to do this?


As it's now clear I was doing it all wrong, I'd still like to know how one could mock the behaviour of before_filter that sets an instance variable. Surely it must be a possible to do this in a controller spec?

like image 826
mnylen Avatar asked Jul 07 '10 10:07

mnylen


1 Answers

Testing the filter is looking too closely at the implementation, IMO. You want to ensure that the instance variable assignment is happening, not whether it takes place within a filter or within an action -- test the outcome, not the implementation.

like image 110
zetetic Avatar answered Sep 20 '22 23:09

zetetic