ruby-on-rails-3 – 如何在rails 3中没有观察者运行rspec?

我有一个有一些观察者的rails3应用程序.我不能为我的生活找出如何把这些关闭我的rspec测试!

解决方法

与Rails 3.1一起使用时,no_peeping_toms输出废弃警告.它目前有 7 pull requests打开以删除这些弃用警告,但是 gem is not necessary with Rails 3.1+.Rails 3.1添加到ActiveModel(因此ActiveRecord)启用和 disable观察者的能力.

您可以将以下行放在spec_helper中以关闭所有ActiveRecord-descendant模型上的所有观察者:

# 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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...