我在spec_helper.rb中设置了以下行
config.use_transactional_fixtures = true
这意味着每个测试都应该自行清理。一次测试所做的任何数据库更新都不应该用于下一次测试。
我的一个spec文件中有两个测试。
it 'should update the DB' do
Setting.put('abcd', 'efgh')
end
it 'should not find the DB update' do
Setting.get('abcd').should be_nil
end
以上两个测试过去常用于Rails 3.2.14
但升级到Rails 4后,第二次测试失败,出现以下错误,
------
expected: nil
got: "efgh"
-----
由于这个问题,我在套件中有大约100个测试失败。
我可以为Rails 4升级找到的唯一相关文档非常模糊: “Rails 4.0已弃用ActiveRecord :: Fixtures,转而使用ActiveRecord :: FixtureSet。”
我不确定这是否相关/如何相关。理想情况下,我希望有一个全局设置(config.use_transactional_fixtures = true),而不必更改测试的逻辑(或在(:each)/ after(:each)模块之前添加额外的模块,以使现有测试通过请帮帮忙!
答案 0 :(得分:2)
我在rails 4.1上遇到了完全相同的问题(使用rspec-rails 3.1)。没有自动运行,虽然我确实有弹簧,这可能是罪魁祸首。 但是,我决定尝试一种替代方案,它工作得很好:数据库清理器,它做了类似的工作: https://github.com/DatabaseCleaner/database_cleaner
添加到Gemfile:
group :test do
...
gem 'database_cleaner'
...
end
然后转到rails helper:
#Database cleaning
config.use_transactional_fixtures = false #IMPORTANT, make sure that rails doesn't try and clean it
config.before(:suite) do
DatabaseCleaner.strategy = :transaction #usually use transaction as the strategy - this is what transaction_fixtures does
DatabaseCleaner.clean_with(:truncation) # I like to ensure my database is in clean state to start with so I truncate at the start of the suite - this is optional.
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
答案 1 :(得分:1)
我删除
require 'rspec/autorun'
来自spec_helper的让事情变得很好。
我运行了zeus服务器。症状是如果我运行测试与zeus服务器事务将保存到测试数据库。
当zeus服务器没有运行时,rspec可以正常工作。
要使它与Zeus服务器一起使用,我必须自动运行。
答案 2 :(得分:0)
正如一条评论所说,这可能是rspec-rails(https://github.com/rails/rails/issues/10376)的一个问题。尝试升级到rspec-rails 2.13.1
或更高版本:
bundle update --source rspec-rails
答案 3 :(得分:0)
我不确定,这是同样的问题。但对我来说,解决方案是 - 仅在"中创建数据"做/结束块。如果你在上下文中创建数据,它就无法工作。
该作品:
context "with array of both exista and non exist words" do
clean_words = ["foo", "bar", "foobar"]
language = "eng"
it "return array of word, that exist in Word class" do
word = FactoryGirl.create(:word)
word2 = FactoryGirl.create(:word, name: "bar")
exist_words = [word, word2]
expect(Word.generate_list_of_exist_words(clean_words, language).sort).to eq exist_words.sort
end
end
这不起作用:
context "with array of both exista and non exist words" do
clean_words = ["foo", "bar", "foobar"]
word = FactoryGirl.create(:word)
word2 = FactoryGirl.create(:word, name: "bar")
exist_words = [word, word2]
language = "eng"
it "return array of word, that exist in Word class" do
expect(Word.generate_list_of_exist_words(clean_words, language).sort).to eq exist_words.sort
end
end