2017-02-20 82 views
1

我有以下的RSpec測試:Rspec的測試失敗了奇怪的原因

describe 'regular user' do 
    let!(:organization) { FactoryGirl.create(:full_organization) } 
    let(:user) { create(:user, organization: organization) } 

    context 'no access' do 
    before do 
     sign_in user 
     binding.pry # debug 
     get(suggestions_path) 
    end 

    it { expect(response).to have_http_status(:redirect) } 
    end 
end 

它工作正常,如果我運行它沒有其他的測試,但如果我運行所有測試失敗。我不知道它如何受到影響。我有DatabaseCleaner有以下設置:

config.before(:all) do 
    DatabaseCleaner.clean_with :truncation # clean DB of any leftover data 
    Rails.application.load_seed 
    end 

    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.around(:each) do |example| 
    DatabaseCleaner.cleaning do 
     example.run 
    end 
    end 

好了,大家都在binding.pry行,我們有一個用戶:

$ user.inspect 
#<User id: 15, organization_id: 1, (...params...)> 

但是,如果我們輸入get(suggestions_path),本次測試失敗這個錯誤:

$ get(suggestions_path)

Module::DelegationError: User#used_trial? delegated to organization.used_trial?, but organization is nil: User id: 38, email: "[email protected]", organization_id: 16, (...params...) from .../app/models/user.rb:34:in `rescue in used_trial?'

如果我再次運行它,它工作正常:

get(suggestions_path) => 302

此ID = 38的用戶不存在,看起來像是由DatabaseCleaner刪除的。但是我不知道爲什麼它在我得到(suggestions_path)請求時仍然存在。

我試圖config.cache_classes = false但它仍然無法:(

+0

如果將'let(:user)'更改爲'let!(:user)' - 錯誤消失了嗎? –

+0

你可以添加'user'和'organization'模型'代碼嗎? – VAD

回答

0

好吧,我已經解決了這個。

,直到我得到一套最低規格與這個錯誤我已經刪除了所有的規格。

然後我試圖逐行刪除,直到找到導致此問題的行:那是sign_in user行。

我已將以下代碼添加到rails_helper.rb:

config.after :each do 
    Warden.test_reset! 
    end 

現在工作正常。