rspec helper user_signed_in?

时间:2016-02-03 02:41:12

标签: ruby-on-rails rspec devise

I have a helper method that returns one link if user_signed_in?. I'm using Devise 2.2.8, Rails 3.2.2 and Rspec 3.4.0.

application_helper.rb

def session_button
  if user_signed_in?
    link_to "Restricted Area", user_index_path
  else
    link_to "Login", new_user_session_path
  end
end

And my test:

application_helper_spec.rb

describe "#session_button" do
  context "user signed in" do
    login_user

    it "returns a link to Restricted Area" do
      expect(session_button).to include "Área Restrita"
      expect(session_button).to include user_index_path
    end
  end

  context "user not signed in" do
    it "returns a link to New Session" do
      expect(session_button).to include "Login"
      expect(session_button).to include new_user_session_path
    end
  end
end

login_user is a macro included in my tests. I also loaded TestHelpers.

RSpec.configure do |config|
  config.include Devise::TestHelpers, type: :helper
  config.extend ControllerMacros, :type => :helper
end

The problem: I'm getting this error:

1) ApplicationHelper#session_button user signed in returns a link to Restricted Area
   Failure/Error: if user_signed_in?

   NoMethodError:
     undefined method `user_signed_in?' for #<RSpec::ExampleGroups::ApplicationHelper::SessionButton::UserSignedIn:0x0000000795a608>

I also tried to stub the method

allow(helper).to receive(:user_signed_in?).and_return(true)

but I get the same error.

How do I stub this devise helper?

1 个答案:

答案 0 :(得分:1)

I found a way using anonymous helper but I don't know if there is a better way...

:%s/\<\(\w\)\(\w\w\+\)\>/\1{{c1::\2}}/g