2010-10-18 35 views

回答

5

兩個選項可能是:

  1. No peeping toms
  2. 以下內容添加到您的test.rb環境:使用Rails 3.1+使用時 config.active_record.observers = []
30

no_peeping_toms將輸出廢棄警告。目前它已打開7 pull requests以刪除這些棄用警告,但是gem is not necessary with Rails 3.1+。 Rails 3.1添加到ActiveModel(因此ActiveRecord)啓用和觀察者的能力。

你可以把下面的行spec_helper關閉所有的觀察家都ActiveRecord的後裔型號:

# spec/spec_helper.rb 
... 
RSpec.configure do |config| 
    ... 
    config.before do 
    ... 
    ActiveRecord::Base.observers.disable :all # <-- Turn 'em all off! 
    end 
end 

你可以把他們重新選擇在您的規格與包裝的動作,以測試他們的行爲enable方法。

# spec/models/foo_observer_spec.rb 
describe FooObserver do 
    subject { FooObserver.instance } 

    it 'notices when new Foos are created' do 
    subject.should_receive(:after_create) 

    Foo.observers.enable :foo_observer do # <- Turn FooObserver on 
     Foo.create('my new foo') 
    end         # <- ... and then back off 
    end 
end 
+0

感謝您指向'Foo.observers.enable' – iRonin 2012-09-11 06:45:13

+0

不客氣! – 2012-09-27 02:37:36