Почему after_commit не работает даже с use_transactional_fixtures = false
Транзакционные светильники в rspec предотвращают вызов after_commit, но даже если я отключу их с помощью
RSpec.configure do |config|
config.use_transactional_fixtures = false
end
after_commit callback
не запускается.
Вот приложение rails с последними rspec/rails, которые я выпустил:
git://github.com/sheabarton/after_commit_demo.git
Ответы
Ответ 1
Один из способов - вызвать вызовы 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
Немного поздно, но надеюсь, что это поможет другим.
Ответ 2
В моем случае я решил эту проблему с настройками 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
Благодаря Тестирование after_commit/after_transaction с Rspec
Ответ 3
Это похоже на ответ @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
Ответ 4
Если вы используете database_cleaner, вы все равно столкнетесь с этим. Я использую test_after_commit драгоценный камень, и это, кажется, делает трюк для меня.
Ответ 5
Этот Gist помог мне.
Это обезьяна - исправляет ActiveRecord, чтобы запускать обратные вызовы after_commit даже при использовании транзакционных светильников.
module ActiveRecord
module ConnectionAdapters
module DatabaseStatements
#
# Run the normal transaction method; when it done, check to see if there
# is exactly one open transaction. If so, that the transactional
# fixtures transaction; from the model 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 автоматически загрузит его в тестовую среду.