2010-10-18 61 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

相关问题