diff options
| author | Robb Kidd <robb@thekidds.org> | 2012-06-12 14:27:03 -0400 |
|---|---|---|
| committer | Robb Kidd <robb@thekidds.org> | 2012-06-20 14:09:46 -0400 |
| commit | dfb5da9da339096ad0a8e754f4f42ca2007b5ae6 (patch) | |
| tree | 3d5464ebdcf31bd3bc3cf44a35e072aa1cde3849 /spec | |
| parent | 5303cc285a2067b59f1e8b68f707e8dbf90fe59e (diff) | |
| download | gitlab-ce-dfb5da9da339096ad0a8e754f4f42ca2007b5ae6.tar.gz | |
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
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/models/activity_observer_spec.rb | 16 | ||||
| -rw-r--r-- | spec/models/issue_observer_spec.rb | 12 | ||||
| -rw-r--r-- | spec/models/project_spec.rb | 21 | ||||
| -rw-r--r-- | spec/requests/admin/admin_users_spec.rb | 22 | ||||
| -rw-r--r-- | spec/requests/issues_spec.rb | 20 | ||||
| -rw-r--r-- | spec/spec_helper.rb | 1 |
6 files changed, 62 insertions, 30 deletions
diff --git a/spec/models/activity_observer_spec.rb b/spec/models/activity_observer_spec.rb index 91c7d9147b2..aed1b26d306 100644 --- a/spec/models/activity_observer_spec.rb +++ b/spec/models/activity_observer_spec.rb @@ -9,9 +9,11 @@ describe ActivityObserver do end describe "Merge Request created" do - before do - @merge_request = Factory :merge_request, :project => project - @event = Event.last + before do + MergeRequest.observers.enable :activity_observer do + @merge_request = Factory :merge_request, :project => project + @event = Event.last + end end it_should_be_valid_event @@ -20,9 +22,11 @@ describe ActivityObserver do end describe "Issue created" do - before do - @issue = Factory :issue, :project => project - @event = Event.last + before do + Issue.observers.enable :activity_observer do + @issue = Factory :issue, :project => project + @event = Event.last + end end it_should_be_valid_event diff --git a/spec/models/issue_observer_spec.rb b/spec/models/issue_observer_spec.rb index 42cf81c5fff..b66803e6868 100644 --- a/spec/models/issue_observer_spec.rb +++ b/spec/models/issue_observer_spec.rb @@ -13,7 +13,10 @@ describe IssueObserver do it 'is called when an issue is created' do subject.should_receive(:after_create) - Factory.create(:issue, :project => Factory.create(:project)) + + Issue.observers.enable :issue_observer do + Factory.create(:issue, :project => Factory.create(:project)) + end end it 'sends an email to the assignee' do @@ -40,8 +43,11 @@ describe IssueObserver do it 'is called when an issue is changed' do changed = Factory.create(:issue, :project => Factory.create(:project)) subject.should_receive(:after_update) - changed.description = 'I changed' - changed.save + + Issue.observers.enable :issue_observer do + changed.description = 'I changed' + changed.save + end end context 'a reassigned email' do diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 6285a852c23..d28668b2445 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -72,16 +72,29 @@ describe Project do end describe "last_activity" do - let(:project) { Factory :project } + let(:project) { Factory :project } + let(:last_event) { double } before do - @issue = Factory :issue, :project => project + project.stub(:events).and_return( [ double, double, last_event ] ) end - it { project.last_activity.should == Event.last } - it { project.last_activity_date.to_s.should == Event.last.created_at.to_s } + it { project.last_activity.should == last_event } 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 let(:project) { Factory :project } diff --git a/spec/requests/admin/admin_users_spec.rb b/spec/requests/admin/admin_users_spec.rb index c98ed2cf6db..d9c3472d7e3 100644 --- a/spec/requests/admin/admin_users_spec.rb +++ b/spec/requests/admin/admin_users_spec.rb @@ -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 diff --git a/spec/requests/issues_spec.rb b/spec/requests/issues_spec.rb index aa43b997f8a..2c8650a8402 100644 --- a/spec/requests/issues_spec.rb +++ b/spec/requests/issues_spec.rb @@ -128,18 +128,22 @@ describe "Issues" do end it "should call send mail" do - Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true)) - click_button "Submit new issue" + Issue.observers.enable :issue_observer do + Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true)) + click_button "Submit new issue" + end end it "should send valid email to user" do - with_resque do - click_button "Submit new issue" + Issue.observers.enable :issue_observer do + with_resque do + click_button "Submit new issue" + end + issue = Issue.last + email = ActionMailer::Base.deliveries.last + email.subject.should have_content("New Issue was created") + email.body.should have_content(issue.title) end - issue = Issue.last - email = ActionMailer::Base.deliveries.last - email.subject.should have_content("New Issue was created") - email.body.should have_content(issue.title) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 18b7854d102..5556798f511 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -52,6 +52,7 @@ RSpec.configure do |config| DatabaseCleaner.start WebMock.disable_net_connect!(allow_localhost: true) + ActiveRecord::Base.observers.disable :all end config.after do |
