How To Disable an Observer in Rails

I wanted to disable an ActiveRecord::Observer during a migration in my Rails application. For the record, simply:
Payment.delete_observers

Update: October 17, 2009

The previous code is problematic, it will disable the PaymentObserver for the entire test run.

I've now switched to the following technique:
class OrderTest < ActiveSupport::TestCase
include FlexMock::TestCase

context "with observers disabled" do
setup do
# Disable order updates made by PaymentObserver
flexmock(PaymentObserver.instance, :after_create => true)
end

context "...more tests..." do
end
end
end


You can adapt to whatever mocking framework you're using. You simply mock any methods in your observer class that you don't want called.

0 comments:

Post a Comment