0

Simple question.

I have a method in my ApplicationHelper that calls my SessionsHelper to load the current_user

i.e.

module ApplicationHelper
  def  some_helper_method
    if current_user.respond_to? :some_method
     #does stuff
    end
  end
end


module SessionsHelper
  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find(...)
  end

This works fine in my running application. However when running from Rspect the ApplicationHelper method cannot find current_user method. In the running app I know the method is available by some rails automagic class loading. But not sure what the best way is to make this work in Rspec.

dboyd68
  • 1,104
  • 15
  • 33
  • It wouldn't be able to find it in the above code either. How does ApplicationHelper "know" that current_user is in SessionHelper – Tony Hopkinson Jan 20 '14 at 23:45

1 Answers1

0

There are multiple ways to handle this issue, but let me give you some background first.

You can configure rails to tell it what kind of helpers you are going to expose to your controllers and within your views with include_all_helpers.

In the old days you had a call to helper :all in your ApplicationController.

So that's how those methods get exposed.

Back to your question:

Solution numero uno: helper.stub(current_user: build(:user))

Solution numero due: helper.extend UserHandling

Prego

Community
  • 1
  • 1
phoet
  • 18,688
  • 4
  • 46
  • 74