Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Unable to stub controller helper method when it is already stubbed on the controller #135

Closed
bradseefeld opened this Issue · 5 comments

5 participants

@bradseefeld

See the stack overflow post: http://stackoverflow.com/questions/10537932/unable-to-stub-helper-method-with-rspec

Specifically:

describe MyModuleHelper do
  it "can stub a helper method" do
    controller.stub!(:current_user).and_return(user)
    helper.stub!(:current_user).and_return(user)
    helper.respond_to?(:current_user).should be_true # Fails
  end
end

In this example, current_user is a helper_method defined on the controller.

@justinko

The controller in your helper spec is not a "RSpec controller" (instance of one of your app's controllers), it is an instance of ActionView::TestCase::TestController. Not sure what Rails is doing to cause the stubbed method to be "removed". Maybe we should warn or raise an exception if controller is called within a helper spec.... @dchelimsky

I would avoid calling controller in any of your helper specs - what controller would it be anyway?

Closing this. If you find the cause, please report back here.

@justinko justinko closed this
@dchelimsky
Owner

@justinko is correct that there should not be any references to controller in helper specs (or view specs, for that matter).

@jdickey

Understand the bit about "don't reference controller in view specs", but still unclear on how I can mock or stub an ApplicationHelper method in a view spec. I want to verify that the helper is called n times while rendering the view, and I've been flailing for a couple of hours here.

@myronmarston

@jdickey -- this sounds like an rspec-rails issue. Can you report it there?

@jdickey

@myronmarston I'd think I'd use rspec-mocks to do it, but I will ask over there. Thanks or the quick reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.