Ruby on rails Rspec:事务性装置在升级到rails 4后不工作
我在spec_helper.rb中设置了以下行Ruby on rails Rspec:事务性装置在升级到rails 4后不工作,ruby-on-rails,activerecord,rspec,Ruby On Rails,Activerecord,Rspec,我在spec_helper.rb中设置了以下行 config.use_transactional_fixtures = true 这意味着每个测试都应该在其自身之后进行清理。一个测试所做的任何db更新都不应该出现在下一个测试中 我的一个规范文件中有两个测试 it 'should update the DB' do Setting.put('abcd', 'efgh') end it 'should not find the DB update' do Setting.get('abc
config.use_transactional_fixtures = true
这意味着每个测试都应该在其自身之后进行清理。一个测试所做的任何db更新都不应该出现在下一个测试中
我的一个规范文件中有两个测试
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),而不必更改测试逻辑(或添加额外的before(:each)/before(:each)模块以使现有测试通过。请帮助!正如一条评论所建议的,这可能是rspec rails()。请尝试升级到rspec rails 2.13.1或更高版本:
bundle更新——源rspec-rails
供我删除
require 'rspec/autorun'
来自spec_的助手使事情顺利进行
我让zeus服务器运行。症状是如果我用zeus服务器运行测试,事务将保存到测试数据库中
当zeus服务器不运行时,rspec可以正常工作
为了让它与Zeus服务器一起工作,我必须自动运行。我在rails 4.1上遇到了完全相同的问题(rspec rails 3.1)。没有自动运行,尽管我有spring,这可能是罪魁祸首。 但是,我决定尝试另一种效果很好的方法:数据库清理器,它可以做类似的工作: 因此,请将以下内容添加到文件:
group :test do
...
gem 'database_cleaner'
...
end
然后更改为rails帮助程序:
#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
我不确定这是否是同一个问题。但对我来说,解决方案是——只在“it”do/end块中创建数据。如果在上下文中创建数据,则无法工作 这就是工作:
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
rspec-rails
似乎有问题。请尝试升级到rspec-rails 2.13.1
。也看看这个问题->@Vimsha,我尝试了2.13.1和2.14.1,但问题似乎仍然存在(相关?放置RSpec.configuration.use_transactional_fixture
-检查它是否真实。您是否也在进行功能测试?如果您关闭它以进行其他操作,它可能会泄漏。如果您可以发布您的spec_helper
和setting.rb
文件,这将非常有用。是的,我正在外部创建数据it
块,因此它是持久的。如果适用,我建议在do块之前将此类数据包装在中