繁体   English   中英

如何在帮助器中访问test(设计)current_user?

[英]How to access to test (devise) current_user in a helper?

我在帮助器中使用了current_user,如下所示:

module InvestorsHelper
  def single?
    binding.pry
    current_user.kyc.marital_status == 'Single'
  end

  def married?
    current_user.kyc.marital_status == 'Married'
  end

  def politically_exposed?
    current_user.kyc.is_politically_exposed.present?
  end

  def tax_residency?
    current_user.kyc.is_tax_residency.present?
  end
end

但是我如何在rspec测试中测试这些方法?

我写了以下rspec代码:

context "Investors Helper" do
    before(:each) do
      puts "Befpre exljfgd"
      current_user =    @user ||= FactoryGirl.create(:user)
      @kyc  ||= FactoryGirl.create(:kyc, user_id: @user.id)
    end

  describe InvestorsHelper, '#single?' do
    it 'returns status of current user' do

      result = InvestorsHelper.single?
      binding.pry
    end
end

结束

但它返回错误:NameError:

   undefined local variable or method `current_user' for InvestorsHelper:Module

我究竟做错了什么?

在您的示例中, InvestorsHelper未定义current_user ,因此您将收到该错误消息。 看起来InvestorsHelper是混合使用的,所以我要么测试它混合的内容,要么将它混合到一个定义current_user并测试它的模拟类中。

另外,我很惊讶您的代码在那里失败而不是在InvestorsHelper.single? 调用这样的方法会尝试调用singleton方法::single? (通常用def self.single?定义def self.single? )但是你已经在代码中将它声明为实例方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM