Commit dfb5da9da339096ad0a8e754f4f42ca2007b5ae6
1 parent
5303cc28
Exists in
master
and in
4 other branches
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
Showing
6 changed files
with
62 additions
and
30 deletions
Show diff stats
spec/models/activity_observer_spec.rb
@@ -9,9 +9,11 @@ describe ActivityObserver do | @@ -9,9 +9,11 @@ describe ActivityObserver do | ||
9 | end | 9 | end |
10 | 10 | ||
11 | describe "Merge Request created" do | 11 | describe "Merge Request created" do |
12 | - before do | ||
13 | - @merge_request = Factory :merge_request, :project => project | ||
14 | - @event = Event.last | 12 | + before do |
13 | + MergeRequest.observers.enable :activity_observer do | ||
14 | + @merge_request = Factory :merge_request, :project => project | ||
15 | + @event = Event.last | ||
16 | + end | ||
15 | end | 17 | end |
16 | 18 | ||
17 | it_should_be_valid_event | 19 | it_should_be_valid_event |
@@ -20,9 +22,11 @@ describe ActivityObserver do | @@ -20,9 +22,11 @@ describe ActivityObserver do | ||
20 | end | 22 | end |
21 | 23 | ||
22 | describe "Issue created" do | 24 | describe "Issue created" do |
23 | - before do | ||
24 | - @issue = Factory :issue, :project => project | ||
25 | - @event = Event.last | 25 | + before do |
26 | + Issue.observers.enable :activity_observer do | ||
27 | + @issue = Factory :issue, :project => project | ||
28 | + @event = Event.last | ||
29 | + end | ||
26 | end | 30 | end |
27 | 31 | ||
28 | it_should_be_valid_event | 32 | it_should_be_valid_event |
spec/models/issue_observer_spec.rb
@@ -13,7 +13,10 @@ describe IssueObserver do | @@ -13,7 +13,10 @@ describe IssueObserver do | ||
13 | 13 | ||
14 | it 'is called when an issue is created' do | 14 | it 'is called when an issue is created' do |
15 | subject.should_receive(:after_create) | 15 | subject.should_receive(:after_create) |
16 | - Factory.create(:issue, :project => Factory.create(:project)) | 16 | + |
17 | + Issue.observers.enable :issue_observer do | ||
18 | + Factory.create(:issue, :project => Factory.create(:project)) | ||
19 | + end | ||
17 | end | 20 | end |
18 | 21 | ||
19 | it 'sends an email to the assignee' do | 22 | it 'sends an email to the assignee' do |
@@ -40,8 +43,11 @@ describe IssueObserver do | @@ -40,8 +43,11 @@ describe IssueObserver do | ||
40 | it 'is called when an issue is changed' do | 43 | it 'is called when an issue is changed' do |
41 | changed = Factory.create(:issue, :project => Factory.create(:project)) | 44 | changed = Factory.create(:issue, :project => Factory.create(:project)) |
42 | subject.should_receive(:after_update) | 45 | subject.should_receive(:after_update) |
43 | - changed.description = 'I changed' | ||
44 | - changed.save | 46 | + |
47 | + Issue.observers.enable :issue_observer do | ||
48 | + changed.description = 'I changed' | ||
49 | + changed.save | ||
50 | + end | ||
45 | end | 51 | end |
46 | 52 | ||
47 | context 'a reassigned email' do | 53 | context 'a reassigned email' do |
spec/models/project_spec.rb
@@ -72,16 +72,29 @@ describe Project do | @@ -72,16 +72,29 @@ describe Project do | ||
72 | end | 72 | end |
73 | 73 | ||
74 | describe "last_activity" do | 74 | describe "last_activity" do |
75 | - let(:project) { Factory :project } | 75 | + let(:project) { Factory :project } |
76 | + let(:last_event) { double } | ||
76 | 77 | ||
77 | before do | 78 | before do |
78 | - @issue = Factory :issue, :project => project | 79 | + project.stub(:events).and_return( [ double, double, last_event ] ) |
79 | end | 80 | end |
80 | 81 | ||
81 | - it { project.last_activity.should == Event.last } | ||
82 | - it { project.last_activity_date.to_s.should == Event.last.created_at.to_s } | 82 | + it { project.last_activity.should == last_event } |
83 | end | 83 | end |
84 | 84 | ||
85 | + describe 'last_activity_date' do | ||
86 | + let(:project) { Factory :project } | ||
87 | + | ||
88 | + it 'returns the creation date of the project\'s last event if present' do | ||
89 | + last_event = double(:created_at => 'now') | ||
90 | + project.stub(:events).and_return( [double, double, last_event] ) | ||
91 | + project.last_activity_date.should == last_event.created_at | ||
92 | + end | ||
93 | + | ||
94 | + it 'returns the project\'s last update date if it has no events' do | ||
95 | + project.last_activity_date.should == project.updated_at | ||
96 | + end | ||
97 | + end | ||
85 | describe "fresh commits" do | 98 | describe "fresh commits" do |
86 | let(:project) { Factory :project } | 99 | let(:project) { Factory :project } |
87 | 100 |
spec/requests/admin/admin_users_spec.rb
@@ -40,19 +40,23 @@ describe "Admin::Users" do | @@ -40,19 +40,23 @@ describe "Admin::Users" do | ||
40 | end | 40 | end |
41 | 41 | ||
42 | it "should call send mail" do | 42 | it "should call send mail" do |
43 | - Notify.should_receive(:new_user_email).and_return(stub(:deliver => true)) | ||
44 | - click_button "Save" | 43 | + User.observers.enable :mailer_observer do |
44 | + Notify.should_receive(:new_user_email).and_return(stub(:deliver => true)) | ||
45 | + click_button "Save" | ||
46 | + end | ||
45 | end | 47 | end |
46 | 48 | ||
47 | it "should send valid email to user with email & password" do | 49 | it "should send valid email to user with email & password" do |
48 | - with_resque do | ||
49 | - click_button "Save" | 50 | + User.observers.enable :mailer_observer do |
51 | + with_resque do | ||
52 | + click_button "Save" | ||
53 | + end | ||
54 | + user = User.last | ||
55 | + email = ActionMailer::Base.deliveries.last | ||
56 | + email.subject.should have_content("Account was created") | ||
57 | + email.body.should have_content(user.email) | ||
58 | + email.body.should have_content(@password) | ||
50 | end | 59 | end |
51 | - user = User.last | ||
52 | - email = ActionMailer::Base.deliveries.last | ||
53 | - email.subject.should have_content("Account was created") | ||
54 | - email.body.should have_content(user.email) | ||
55 | - email.body.should have_content(@password) | ||
56 | end | 60 | end |
57 | end | 61 | end |
58 | 62 |
spec/requests/issues_spec.rb
@@ -128,18 +128,22 @@ describe "Issues" do | @@ -128,18 +128,22 @@ describe "Issues" do | ||
128 | end | 128 | end |
129 | 129 | ||
130 | it "should call send mail" do | 130 | it "should call send mail" do |
131 | - Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true)) | ||
132 | - click_button "Submit new issue" | 131 | + Issue.observers.enable :issue_observer do |
132 | + Notify.should_receive(:new_issue_email).and_return(stub(:deliver => true)) | ||
133 | + click_button "Submit new issue" | ||
134 | + end | ||
133 | end | 135 | end |
134 | 136 | ||
135 | it "should send valid email to user" do | 137 | it "should send valid email to user" do |
136 | - with_resque do | ||
137 | - click_button "Submit new issue" | 138 | + Issue.observers.enable :issue_observer do |
139 | + with_resque do | ||
140 | + click_button "Submit new issue" | ||
141 | + end | ||
142 | + issue = Issue.last | ||
143 | + email = ActionMailer::Base.deliveries.last | ||
144 | + email.subject.should have_content("New Issue was created") | ||
145 | + email.body.should have_content(issue.title) | ||
138 | end | 146 | end |
139 | - issue = Issue.last | ||
140 | - email = ActionMailer::Base.deliveries.last | ||
141 | - email.subject.should have_content("New Issue was created") | ||
142 | - email.body.should have_content(issue.title) | ||
143 | end | 147 | end |
144 | 148 | ||
145 | end | 149 | end |
spec/spec_helper.rb
@@ -52,6 +52,7 @@ RSpec.configure do |config| | @@ -52,6 +52,7 @@ RSpec.configure do |config| | ||
52 | DatabaseCleaner.start | 52 | DatabaseCleaner.start |
53 | 53 | ||
54 | WebMock.disable_net_connect!(allow_localhost: true) | 54 | WebMock.disable_net_connect!(allow_localhost: true) |
55 | + ActiveRecord::Base.observers.disable :all | ||
55 | end | 56 | end |
56 | 57 | ||
57 | config.after do | 58 | config.after do |