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

时间:2016-07-05 12:18:40

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

我在帮助器中使用了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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

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

此外,我很惊讶您的代码在那里失败而不在::single?。调用这样的方法会尝试调用单例方法def self.single?(通常用func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == UIWebViewNavigationType.LinkClicked { self.showLinksClicked() return false } return true; } func showLinksClicked() { let safariVC = SFSafariViewController(URL: NSURL(string: "www.example.com")!) self.presentViewController(safariVC, animated: true, completion: nil) safariVC.delegate = self } func safariViewControllerDidFinish(controller: SFSafariViewController) { controller.dismissViewControllerAnimated(true, completion: nil) } 定义)但你已经在代码中将它声明为实例方法。