2

我升級從Rails 3中的應用Rails的4時,有一個叫項目以下架構模型:RSpec的測試失敗,FactoryGirl,通過隔離

#===Schema Information 
# Table name: items 
# 
#type :string 
#maturity: string 

,並與一系列的規範測試,但是這一次,特別是我有問題有:

describe 'next_version' do 
it 'should return the created_at date of the Item one version higher' do 
first = FactoryGirl.create(:item, maturity: 'PRODUCTION') 
second = FactoryGirl.create(:item, maturity: 'PREPRODUCTION', created_at: Time.now.in_time_zone) 

expect(first.next_version_created_at.to_s)to eq second.created_at.utc.to_s) 

的修復,我認爲這是很容易..只是添加..

created_at: Time.now.in_time_zone 

..到生產項目並完成。然而,如果我自己運行測試,並且整個規範在Rails 3中傳遞(146次測試),但是當我現在運行整個規範時,這一個和另外三個類似的情況會失敗,除非我單獨運行它們。

想法?

回答

1

通常在測試數據庫中沒有正確清理數據時會發生這種情況。

請確保您有:

config.use_transactional_fixtures = false

在spec_helper.rb或rails_helper.rb

在這裏看到:

rspec test passes in isolation, but fails when run with other tests

RSpec errors when run en masse, but not individually

+0

我很高興你指出了這一點,不過,該行已經在spec_helper.rb,並且我們已經使用DatabaseCleaner ...任何其他的想法? – drthvdr

2

你可以嘗試每次測試之前重裝工廠女孩。嘗試添加該到spec_helper.rb

config.before(:each) do 
    FactoryGirl.reload 
end