Limit controller action in Rspec controller test, but it is still executed

I would like to check if the action of the controller was actually called without redirecting in some before_filter file. Since the controller action itself can do a redirect, I want to stub the action to raise a specific error (SuccessfulActionError or the like), and then check this error as an indicator that the method has been called.

So, I added the following:

controller.stub!(:action).and_raise(SuccessfulActionError) 

Somehow it works, an exception occurs, but the actual code in the method is still executing (for example, if I send the identifier of a nonexistent record to the "show" action, it throws an ActiveRecord :: RecordNotFound exception).

Why? I want to completely close the action, as if it were implemented as

 def action raise SuccessfulActionError end 

What am I doing wrong? Is this the wrong approach?

EDIT:

Using

 controller.should_receive(:action) 

doesn't work either.

I am rewriting the controller into the before_all filter like this to fix default_url_options that are not selected from ApplicationController:

 class MyController def default_url_options(options = {}) { :locale => params[:locale] } end end 

Could this be the culprit? Specifications do not work at all when I delete it, unfortunately.

+4
source share
1 answer

It is not clear to me how you can get an ActiveRecord :: RecordNotFound exception after completing this method. A piece should replace a method call.

As an alternative approach, you might consider setting the expected message for the action, for example.

 controller.should_receive(:show) get :show 

which should fail if before_filter does not allow the action to be called.

0
source

Source: https://habr.com/ru/post/1342436/


All Articles