首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >user_signed_in助手?

user_signed_in助手?
EN

Stack Overflow用户
提问于 2016-02-03 02:41:12
回答 1查看 1.1K关注 0票数 1

我有一个返回一个链接if user_signed_in?的助手方法。我使用的是Design2.2.8、Rails 3.2.2和Rspec 3.4.0。

application_helper.rb

代码语言:javascript
运行
复制
def session_button
  if user_signed_in?
    link_to "Restricted Area", user_index_path
  else
    link_to "Login", new_user_session_path
  end
end

我的测试是:

application_helper_spec.rb

代码语言:javascript
运行
复制
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是我的测试中包含的宏。我还加载了TestHelpers

代码语言:javascript
运行
复制
RSpec.configure do |config|
  config.include Devise::TestHelpers, type: :helper
  config.extend ControllerMacros, :type => :helper
end

问题是:我得到了这个错误:

代码语言:javascript
运行
复制
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>

我还试着给这个方法留条。

代码语言:javascript
运行
复制
allow(helper).to receive(:user_signed_in?).and_return(true)

但我也犯了同样的错误。

我该如何处理这个设计帮手呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-12 20:48:11

我找到了一种使用匿名助手的方法,但我不知道有没有更好的方法.

代码语言:javascript
运行
复制
RSpec.describe ApplicationHelper, :type => :helper do
  helper do
    def user_signed_in?
      user.present?
    end
  end

  describe "#session_button" do
    context "user signed in" do
      let(:user) { FactoryGirl.create :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
      let(:user) { nil }

      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
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35167568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档