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