2016-07-05 59 views
1

我有一個助手使用CURRENT_USER如下:如何訪問測試(設計)助手中的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 

我在做什麼錯?

回答

0

在你的例子中InvestorsHelper沒有定義current_user,所以你會得到那個錯誤信息。它看起來像InvestorsHelper是爲了混合,所以我要麼測試它被混入的東西,要麼混合成一個模擬類,定義current_user並測試它。

此外,我很驚訝你的代碼在那裏失敗,而不是在InvestorsHelper.single?。調用這樣的方法將嘗試調用單例方法::single?(通常使用def self.single?定義),但是您已將其聲明爲代碼中的實例方法。