Testing before_filter is called in action

How can I verify that when an action in the controller is available, this before_filter controller will be executed?

The before_filter test is tested in a separate group of examples, so there is no need to duplicate tests for all actions that depend on the before_filter file.

If i have

controller.should_receive(:my_before_filter)

in my example, it works fine. However, the presence of the above expectation, apparently, leads to the fact that the logic is my_before_filternot executed (it should assign an instance variable).

How to overcome this limitation or mock the behavior of my_before_filter (it sets the instance variable on the controller)? Or is there a better way to do this?


As it’s clear now, I did all this wrong, I would still like to know how to make fun of the before_filter behavior that sets the instance variable. Of course, this should be possible to do in the controller specification?

+5
source share
3 answers

Filter testing looks too closely at implementation, IMO. You want to make sure that the assignment of the instance variable occurs, and whether it happens inside the filter or inside the action - test the result, not the implementation.

+6
source

RSpec should_receive implies that you expect to get this method, but as a layout method, so it does not actually call the real method, which in this case is "my_before_filter".

http://axonflux.com/rspecs-shouldreceive-doesnt-ac

my_before_filter , , , .

, , , .

, , , " " .

http://rspec.info/documentation/mocks/message_expectations.html

+3

" , before_filter, ."

:

controller.should_receive(:my_before_filter) 
controller.instance_variable_set(:@my_instance_var, "some value")
+1

All Articles