加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-4 – 使用rails 4和rspec 3通过verify_partial_do

发布时间:2020-12-17 02:23:07 所属栏目:百科 来源:网络整理
导读:我正在使用authlogic进行用户身份验证,而在我的ApplicationController中,我将“current_user”,“current_user_session”等定义并设置为helper_methods. 我的主索引有一个非常简单的视图规范: RSpec.describe "main/index.html.erb",:type = :view do conte
我正在使用authlogic进行用户身份验证,而在我的ApplicationController中,我将“current_user”,“current_user_session”等定义并设置为helper_methods.

我的主索引有一个非常简单的视图规范:

RSpec.describe "main/index.html.erb",:type => :view do
  context "when not logged in" do

    before do
      allow(view).to receive(:current_user).and_return(nil)
    end

    it "has an h1" do
      render
      expect(rendered).to include('h1')
    end

  end
end

问题是,如果我的配置中的“mocks.verify_partial_doubles = true”,那么这会导致一个令人印象深刻的大错误,因为它会转储整个对象然后在底部说:

1) main/index.html.erb when not logged in has an h1
     Failure/Error: allow(view).to receive(:current_user).and_return(nil)
       #<#<Class:0x00000104c249d0>:.........
       @rendered_views={}>> does not implement: current_user

当然,建议将verify_partial_doubles设置为true,但这样做会中断.我直接从文档中提取了这个:

https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/view-specs/view-spec#passing-view-spec-that-stubs-a-helper-method

如果该方法出现在ApplicationHelper中,它将起作用.但是如果它在ApplicationController中并被定义为helper_method则没有这样的运气:

helper_method :current_user,...

def current_user
  return @current_user if defined?(@current_user)
  @current_user = current_user_session && current_user_session.record
end

我想要verify_partial_doubles提供的保护,我该如何解决这个问题?

解决方法

这是一个已知问题,使其工作的唯一方法是将方法提取到模块中并将其包含在视图助手和控制器中.

更多信息:https://github.com/rspec/rspec-rails/issues/1076

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读