Disable observers in specs. Enable only when observer is under test.

Used the built-in observer enable/disable feature in ActiveModel[1].
ActiveRecord::Base includes ActiveModel::Observing which provides this
behavior.

Simple wraps to enable the observer under test were added to the specs
for: ActivityObserver, IssueObserver, Admin::Users and Issues.

The spec for Project.last_activity was refactored to separate the tests
for #last_activity and #last_activity_date. Each had doubles added to
isolate the spec from the hidden dependency on the ActivityObserver
action to create an Event for the project when an Issue is created. This
ActivityObserver behavior is already tested by its spec.

[1] http://api.rubyonrails.org/classes/ActiveModel/ObserverArray.html
This commit is contained in:
Robb Kidd 2012-06-12 14:27:03 -04:00
parent 5303cc285a
commit dfb5da9da3
6 changed files with 62 additions and 30 deletions

View file

@ -40,19 +40,23 @@ describe "Admin::Users" do
end
it "should call send mail" do
Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
click_button "Save"
User.observers.enable :mailer_observer do
Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
click_button "Save"
end
end
it "should send valid email to user with email & password" do
with_resque do
click_button "Save"
User.observers.enable :mailer_observer do
with_resque do
click_button "Save"
end
user = User.last
email = ActionMailer::Base.deliveries.last
email.subject.should have_content("Account was created")
email.body.should have_content(user.email)
email.body.should have_content(@password)
end
user = User.last
email = ActionMailer::Base.deliveries.last
email.subject.should have_content("Account was created")
email.body.should have_content(user.email)
email.body.should have_content(@password)
end
end