[英]Rspec test fails for a strange reason
我有以下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:用戶#used_trial? 委托給organization.used_trial?,但組織是零:用戶ID:38,電子郵件:“person482@example.com”,organization_id:16,(... params ...)來自... / app / models / user .rb:34:在“used_trial中救援?”
如果我再次運行它,它工作正常:
get(suggestions_path)=> 302
ID = 38的用戶不存在,看起來像是被DatabaseCleaner刪除了。 但是我不知道為什么它在我得到(suggestions_path)請求時仍然存在。
我試過config.cache_classes = false
但它仍然失敗:(
好的,我已經解決了這個問題。
我刪除了所有規格,直到我得到這個錯誤的最小設置規格。
然后我嘗試逐行刪除,直到找到導致此問題的行:這是sign_in user
行。
我在rails_helper.rb中添加了以下代碼:
config.after :each do
Warden.test_reset!
end
它現在工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.