15

rspec 中的事务性固定装置阻止调用 after_commit,但即使我禁用它们

RSpec.configure do |config|
  config.use_transactional_fixtures = false
end

after_commit callback不运行。

这是一个带有最新 rspec / rails 的 rails 应用程序,我在此问题上产生了问题: git://github.com/sheabarton/after_commit_demo.git

4

5 回答 5

28

commit解决此问题的一种方法是手动触发回调。例子:

describe SomeModel do
  subject { ... }

  context 'after_commit' do
    after { subject.run_callbacks(:commit) }

    it 'does something' do
      subject.should_receive(:some_message)
    end
  end
end

有点晚了,但希望这对其他人有帮助。

于 2013-05-04T03:08:04.667 回答
9

就我而言,我通过下面的 database_cleaner 设置解决了此类问题:

config.use_transactional_fixtures = false

config.before(:suite) do
  DatabaseCleaner.strategy = :deletion
  DatabaseCleaner.clean_with(:truncation)
end

config.before(:each) do
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

感谢使用Rspec 测试 after_commit/after_transaction

于 2014-05-22T11:59:21.147 回答
8

这类似于上面@jamesdevar 的答案,但我无法添加代码块,所以我必须单独输入。

您没有更改整个规范套件的策略。您可以在:transaction全局范围内继续使用,然后根据需要使用:deletion:truncation(它们都有效)。只需在相关规范中添加一个标志。

config.use_transactional_fixtures = false

config.before(:suite) do
  # The :transaction strategy prevents :after_commit hooks from running
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)
end

config.before(:each, :with_after_commit => true) do
  DatabaseCleaner.strategy = :truncation
end

然后,在您的规格中:

describe "some test requiring after_commit hooks", :with_after_commit => true do
于 2014-11-10T17:24:42.703 回答
5

如果您使用的是database_cleaner,您仍然会遇到这种情况。我正在使用test_after_commit gem,这似乎对我有用。

于 2014-04-01T00:31:21.933 回答
2

这个要点帮助了我。

即使使用事务性固定装置,它也会对 ActiveRecord 进行猴子补丁以触发 after_commit 回调。

module ActiveRecord
  module ConnectionAdapters
    module DatabaseStatements
      #
      # Run the normal transaction method; when it's done, check to see if there
      # is exactly one open transaction. If so, that's the transactional
      # fixtures transaction; from the model's standpoint, the completed
      # transaction is the real deal. Send commit callbacks to models.
      #
      # If the transaction block raises a Rollback, we need to know, so we don't
      # call the commit hooks. Other exceptions don't need to be explicitly
      # accounted for since they will raise uncaught through this method and
      # prevent the code after the hook from running.
      #
      def transaction_with_transactional_fixtures(options = {}, &block)
        rolled_back = false

        transaction_without_transactional_fixtures do
          begin
            yield
          rescue ActiveRecord::Rollback => e
            rolled_back = true
            raise e
          end
        end

        if !rolled_back && open_transactions == 1
          commit_transaction_records(false)
        end
      end
      alias_method_chain :transaction, :transactional_fixtures

      #
      # The @_current_transaction_records is an stack of arrays, each one
      # containing the records associated with the corresponding transaction
      # in the transaction stack. This is used by the
      # `rollback_transaction_records` method (to only send a rollback hook to
      # models attached to the transaction being rolled back) but is usually
      # ignored by the `commit_transaction_records` method. Here we
      # monkey-patch it to temporarily replace the array with only the records
      # for the top-of-stack transaction, so the real
      # `commit_transaction_records` method only sends callbacks to those.
      #
      def commit_transaction_records_with_transactional_fixtures(commit = true)
        unless commit
          real_current_transaction_records = @_current_transaction_records
          @_current_transaction_records = @_current_transaction_records.pop
        end

        begin
          commit_transaction_records_without_transactional_fixtures
        rescue # works better with that :)
        ensure
          unless commit
            @_current_transaction_records = real_current_transaction_records
         end
        end
      end
      alias_method_chain :commit_transaction_records, :transactional_fixtures
    end
  end
end

将这个新文件放在 Rails.root/spec/support 目录中,例如spec/support/after_commit_with_transactional_fixtures.rb.

Rails 3 会在测试环境中自动加载它。

于 2012-07-18T18:22:12.993 回答