简体   繁体   English

Rspec:事务夹具在升级到rails 4后不起作用

[英]Rspec: Transactional fixtures do not work after upgrading to rails 4

I have the following line set in spec_helper.rb 我在spec_helper.rb中设置了以下行

config.use_transactional_fixtures = true

This means that every test should cleanup after itself. 这意味着每个测试都应该自行清理。 Any db update made by one test should not be around for the next test. 一次测试所做的任何db更新都不应该用于下一次测试。

I have two tests in one of my spec files. 我的一个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

The above two test used to work with Rails 3.2.14 以上两个测试用于使用Rails 3.2.14

However after upgrading to Rails 4, the second test fails with the following error, 但升级到Rails 4后,第二次测试失败,出现以下错误,

------
expected: nil
got: "efgh"
-----

I have about a 100 tests failing in the suite because of this issue. 由于这个问题,我在套件中有大约100个测试失败。

The only related documentation I can find for Rails 4 upgrade was something quite vague: "Rails 4.0 has deprecated ActiveRecord::Fixtures in favor of ActiveRecord::FixtureSet." 我可以为Rails 4升级找到的唯一相关文档是非常模糊的:“Rails 4.0已经弃用了ActiveRecord :: Fixtures而支持ActiveRecord :: FixtureSet。”

I am not sure if/how this is relevant. 我不确定这是否/如何相关。 I would ideally like to have a global setting (config.use_transactional_fixtures = true), and not have to change the logic of the tests (or add extra before(:each)/after(:each) modules just to get existing tests to pass. Please help! 理想情况下,我希望有一个全局设置(config.use_transactional_fixtures = true),而不必更改测试的逻辑(或在(:each)/ after(:each)模块之前添加额外的模块,以使现有测试通过。 请帮忙!

I had exactly the same issue (with rspec-rails 3.1) on rails 4.1. 我在rails 4.1上遇到了完全相同的问题(使用rspec-rails 3.1)。 Didn't have auto-run running, although I did have spring, which may be the culprit. 没有自动运行,虽然我确实有弹簧,这可能是罪魁祸首。 However, I decided to try an alternative, which worked nicely: database cleaner, which does a similar job: https://github.com/DatabaseCleaner/database_cleaner 但是,我决定尝试一种替代方案,它工作得很好:数据库清理器,它做了类似的工作: https//github.com/DatabaseCleaner/database_cleaner

So add to Gemfile: 所以添加到Gemfile:

group :test do
...
  gem 'database_cleaner'
...
end

Then change to rails helper: 然后更改为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 

For me removing 为我删除

require 'rspec/autorun'

from spec_helper made things worked fine. 来自spec_helper,事情很好。

I had zeus server running. 我有zeus服务器正在运行。 Symptoms were if i run test with zeus server transaction would get saved into the Test DB. 症状是如果我运行测试与zeus服务器事务将保存到测试数据库中。

When zeus server was not running the rspec would work fine. 当zeus服务器没有运行时,rspec可以正常工作。

To make it work with Zeus server i had to autorun. 为了使它与Zeus服务器一起工作,我必须自动运行。

As one comment suggested, this is likely an issue with rspec-rails ( https://github.com/rails/rails/issues/10376 ). 正如一条评论所说,这可能是rspec-rails的问题( https://github.com/rails/rails/issues/10376 )。 Try upgrading to rspec-rails 2.13.1 or higher: 尝试升级到rspec-rails 2.13.1或更高版本:

bundle update --source rspec-rails

I am not sure, that it's same problem. 我不确定,这是同样的问题。 But for me the solution was - to create data only in "it" do/end block. 但对我来说,解决方案是 - 仅在“it”do / end块中创建数据。 And if you create data in context it doesn't work. 如果你在上下文中创建数据它不起作用。

that's works: 这是有效的:

    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

that's not works: 这不起作用:

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM