我有以下的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
但它仍然無法:(
如果將'let(:user)'更改爲'let!(:user)' - 錯誤消失了嗎? –
你可以添加'user'和'organization'模型'代碼嗎? – VAD