Last Updated: February 25, 2016
·
6.561K
· kareemgrant

Stubbing a helper method in your capybara specs

In your feature specs, you may encounter a situation where you need to stub out a method that's located in one of your helper modules (i.e. ApplicationHelper).

module ApplicationHelper
  def current_company
    current_user.company if current_user
  end
end

In the above example, I need to stub out the current_company method and return an object that responds to the items message.

Attempting to stub out the ApplicationHelper itself doesn't cut it.

For example this won't work:

ApplicationHelper.stub(:current_company){double('company', items: [])}

To get this working, just stub out the the same method as an instance of the ApplicationController:

ApplicationController.any_instance.stub(:current_company){double('company', items: [])}