no-peeping-toms

NOTE: This functionality is included in Rails 3.1 and above. Disables observers during testing, allowing you to write model tests that are completely decoupled from the observer. Also lets you enab…

  • 所有者: patmaddox/no-peeping-toms
  • 平台:
  • 許可證:
  • 分類:
  • 主題:
  • 喜歡:
    2
      比較:

Github星跟蹤圖

= no_peeping_toms

This plugin disables observers in your specs, so that model specs can run in complete isolation.

== NOTE: This functionality is included in Rails 3.1 and above: https://apidock.com/rails/v3.1.0/ActiveModel/ObserverArray/disable. Use that instead!

== Installation

Add to your Gemfile:

gem 'no_peeping_toms', :git => 'git://github.com/patmaddox/no-peeping-toms.git'

and run bundle install.

no_peeping_toms >= 2.0.0 only supports on Rails 3. If you need Rails 2 support, use 1.1.0: gem install no_peeping_toms -v 1.1.0

== Usage

To disable observers, place the following code in your test.rb, or spec_helper.rb, or wherever:

ActiveRecord::Observer.disable_observers

You can easily reenable observers:

ActiveRecord::Observer.enable_observers

You can choose to run some code with specific observers turned on. This is useful when spec'ing an observer. For example, if you write the following observer:

class PersonObserver < ActiveRecord::Observer
def before_update(person)
old_person = Person.find person.id
if old_person.name != person.name
NameChange.create! :person => person, :old_name => old_person.name, :new_name => person.name
end
end
end

You can spec the Person class in complete isolation.

describe Person, " when changing a name" do
before(:each) do
@person = Person.create! :name => "Pat Maddox"
end

# By default, don't run any observers
it "should not register a name change" do
  lambda { @person.update_attribute :name, "Don Juan Demarco" }.should_not change(NameChange, :count)
end

# Run only a portion of code with certain observers turned on
it "should register a name change with the person observer turned on" do
  ActiveRecord::Observer.with_observers(:person_observer) do
    lambda { @person.update_attribute :name, "Don Juan Demarco" }.should change(NameChange, :count).by(1)
  end

  lambda { @person.update_attribute :name, "Man Without a Name" }.should_not change(NameChange, :count)
end

end

== Credits

  • Brandon Keepers
  • Corey Haines
  • Drew Olson
  • Jeff Siegel
  • Zach Dennis
  • Andy Lindeman
  • Ryan McGeary

Copyright (c) 2007-2011 Pat Maddox, released under the MIT license.

主要指標

概覽
名稱與所有者patmaddox/no-peeping-toms
主編程語言Ruby
編程語言Ruby (語言數: 1)
平台
許可證
所有者活动
創建於2008-03-04 16:09:25
推送於2017-09-15 17:45:54
最后一次提交2017-09-15 11:45:53
發布數2
最新版本名稱v1.1.0 (發布於 )
第一版名稱v1.0.1 (發布於 )
用户参与
星數135
關注者數2
派生數39
提交數56
已啟用問題?
問題數11
打開的問題數5
拉請求數6
打開的拉請求數1
關閉的拉請求數11
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?