Commit dfb5da9d authored by Robb Kidd's avatar Robb Kidd

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
parent 5303cc28
...@@ -10,9 +10,11 @@ describe ActivityObserver do ...@@ -10,9 +10,11 @@ describe ActivityObserver do
describe "Merge Request created" do describe "Merge Request created" do
before do before do
MergeRequest.observers.enable :activity_observer do
@merge_request = Factory :merge_request, :project => project @merge_request = Factory :merge_request, :project => project
@event = Event.last @event = Event.last
end end
end
it_should_be_valid_event it_should_be_valid_event
it { @event.action.should == Event::Created } it { @event.action.should == Event::Created }
...@@ -21,9 +23,11 @@ describe ActivityObserver do ...@@ -21,9 +23,11 @@ describe ActivityObserver do
describe "Issue created" do describe "Issue created" do
before do before do
Issue.observers.enable :activity_observer do
@issue = Factory :issue, :project => project @issue = Factory :issue, :project => project
@event = Event.last @event = Event.last
end end
end
it_should_be_valid_event it_should_be_valid_event
it { @event.action.should == Event::Created } it { @event.action.should == Event::Created }
......
...@@ -13,8 +13,11 @@ describe IssueObserver do ...@@ -13,8 +13,11 @@ describe IssueObserver do
it 'is called when an issue is created' do it 'is called when an issue is created' do
subject.should_receive(:after_create) subject.should_receive(:after_create)
Issue.observers.enable :issue_observer do
Factory.create(:issue, :project => Factory.create(:project)) Factory.create(:issue, :project => Factory.create(:project))
end end
end
it 'sends an email to the assignee' do it 'sends an email to the assignee' do
Notify.should_receive(:new_issue_email).with(issue.id) Notify.should_receive(:new_issue_email).with(issue.id)
...@@ -40,9 +43,12 @@ describe IssueObserver do ...@@ -40,9 +43,12 @@ describe IssueObserver do
it 'is called when an issue is changed' do it 'is called when an issue is changed' do
changed = Factory.create(:issue, :project => Factory.create(:project)) changed = Factory.create(:issue, :project => Factory.create(:project))
subject.should_receive(:after_update) subject.should_receive(:after_update)
Issue.observers.enable :issue_observer do
changed.description = 'I changed' changed.description = 'I changed'
changed.save changed.save
end end
end
context 'a reassigned email' do context 'a reassigned email' do
it 'is sent if the issue is being reassigned' do it 'is sent if the issue is being reassigned' do
......
...@@ -73,15 +73,28 @@ describe Project do ...@@ -73,15 +73,28 @@ describe Project do
describe "last_activity" do describe "last_activity" do
let(:project) { Factory :project } let(:project) { Factory :project }
let(:last_event) { double }
before do before do
@issue = Factory :issue, :project => project project.stub(:events).and_return( [ double, double, last_event ] )
end end
it { project.last_activity.should == Event.last } it { project.last_activity.should == last_event }
it { project.last_activity_date.to_s.should == Event.last.created_at.to_s }
end end
describe 'last_activity_date' do
let(:project) { Factory :project }
it 'returns the creation date of the project\'s last event if present' do
last_event = double(:created_at => 'now')
project.stub(:events).and_return( [double, double, last_event] )
project.last_activity_date.should == last_event.created_at
end
it 'returns the project\'s last update date if it has no events' do
project.last_activity_date.should == project.updated_at
end
end
describe "fresh commits" do describe "fresh commits" do
let(:project) { Factory :project } let(:project) { Factory :project }
......
...@@ -40,11 +40,14 @@ describe "Admin::Users" do ...@@ -40,11 +40,14 @@ describe "Admin::Users" do
end end
it "should call send mail" do it "should call send mail" do
User.observers.enable :mailer_observer do
Notify.should_receive(:new_user_email).and_return(stub(:deliver => true)) Notify.should_receive(:new_user_email).and_return(stub(:deliver => true))
click_button "Save" click_button "Save"
end end
end
it "should send valid email to user with email & password" do it "should send valid email to user with email & password" do
User.observers.enable :mailer_observer do
with_resque do with_resque do
click_button "Save" click_button "Save"
end end
...@@ -55,6 +58,7 @@ describe "Admin::Users" do ...@@ -55,6 +58,7 @@ describe "Admin::Users" do
email.body.should have_content(@password) email.body.should have_content(@password)
end end
end end
end
describe "GET /admin/users/:id" do describe "GET /admin/users/:id" do
before do before do
......
...@@ -128,11 +128,14 @@ describe "Issues" do ...@@ -128,11 +128,14 @@ describe "Issues" do
end end
it "should call send mail" do it "should call send mail" do
Issue.observers.enable :issue_observer do
Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true)) Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true))
click_button "Submit new issue" click_button "Submit new issue"
end end
end
it "should send valid email to user" do it "should send valid email to user" do
Issue.observers.enable :issue_observer do
with_resque do with_resque do
click_button "Submit new issue" click_button "Submit new issue"
end end
...@@ -141,6 +144,7 @@ describe "Issues" do ...@@ -141,6 +144,7 @@ describe "Issues" do
email.subject.should have_content("New Issue was created") email.subject.should have_content("New Issue was created")
email.body.should have_content(issue.title) email.body.should have_content(issue.title)
end end
end
end end
end end
......
...@@ -52,6 +52,7 @@ RSpec.configure do |config| ...@@ -52,6 +52,7 @@ RSpec.configure do |config|
DatabaseCleaner.start DatabaseCleaner.start
WebMock.disable_net_connect!(allow_localhost: true) WebMock.disable_net_connect!(allow_localhost: true)
ActiveRecord::Base.observers.disable :all
end end
config.after do config.after do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment