Rspec的测试失败了奇怪的原因

问题描述:

我有以下的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

好吧,我已经解决了这个。

,直到我得到一套最低规格与这个错误我已经删除了所有的规格。

然后我试图逐行删除,直到找到导致此问题的行:那是sign_in user行。

我已将以下代码添加到rails_helper.rb:

config.after :each do 
    Warden.test_reset! 
    end 

现在工作正常。