加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:47:49 所属栏目:百科 来源:网络整理
导读:我有一个有一些观察者的rails3应用程序.我不能为我的生活找出如何把这些关闭我的rspec测试! 解决方法 与Rails 3.1一起使用时,no_peeping_toms将输出废弃警告.它目前有 7 pull requests打开以删除这些弃用警告,但是 gem is not necessary with Rails 3.1+.Ra
我有一个有一些观察者的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读