Commit 001f3bc59ef34752b98ab405fde2520b15eff51c

Authored by Dmitriy Zaporozhets
1 parent 0ae89200

Specs refactoring to reduce test time. Disabled observers by default for specs

spec/features/profile_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe "Profile account page" do 3 describe "Profile account page" do
  4 + before(:each) { enable_observers }
4 let(:user) { create(:user) } 5 let(:user) { create(:user) }
5 6
6 before do 7 before do
spec/features/projects_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe "Projects" do 3 describe "Projects" do
  4 + before(:each) { enable_observers }
4 before { login_as :user } 5 before { login_as :user }
5 6
6 describe "DELETE /projects/:id" do 7 describe "DELETE /projects/:id" do
spec/lib/votes_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 -describe MergeRequest do  
4 - let(:merge_request) { FactoryGirl.create(:merge_request_with_diffs) } 3 +describe Issue, 'Votes' do
  4 + let(:issue) { create(:issue) }
5 5
6 describe "#upvotes" do 6 describe "#upvotes" do
7 it "with no notes has a 0/0 score" do 7 it "with no notes has a 0/0 score" do
8 - merge_request.upvotes.should == 0 8 + issue.upvotes.should == 0
9 end 9 end
10 10
11 it "should recognize non-+1 notes" do 11 it "should recognize non-+1 notes" do
12 - merge_request.notes << create(:note, note: "No +1 here")  
13 - merge_request.should have(1).note  
14 - merge_request.notes.first.upvote?.should be_false  
15 - merge_request.upvotes.should == 0 12 + add_note "No +1 here"
  13 + issue.should have(1).note
  14 + issue.notes.first.upvote?.should be_false
  15 + issue.upvotes.should == 0
16 end 16 end
17 17
18 it "should recognize a single +1 note" do 18 it "should recognize a single +1 note" do
19 - merge_request.notes << create(:note, note: "+1 This is awesome")  
20 - merge_request.upvotes.should == 1 19 + add_note "+1 This is awesome"
  20 + issue.upvotes.should == 1
21 end 21 end
22 22
23 it "should recognize multiple +1 notes" do 23 it "should recognize multiple +1 notes" do
24 - merge_request.notes << create(:note, note: "+1 This is awesome")  
25 - merge_request.notes << create(:note, note: "+1 I want this")  
26 - merge_request.upvotes.should == 2 24 + add_note "+1 This is awesome"
  25 + add_note "+1 I want this"
  26 + issue.upvotes.should == 2
27 end 27 end
28 end 28 end
29 29
30 describe "#downvotes" do 30 describe "#downvotes" do
31 it "with no notes has a 0/0 score" do 31 it "with no notes has a 0/0 score" do
32 - merge_request.downvotes.should == 0 32 + issue.downvotes.should == 0
33 end 33 end
34 34
35 it "should recognize non--1 notes" do 35 it "should recognize non--1 notes" do
36 - merge_request.notes << create(:note, note: "Almost got a -1")  
37 - merge_request.should have(1).note  
38 - merge_request.notes.first.downvote?.should be_false  
39 - merge_request.downvotes.should == 0 36 + add_note "Almost got a -1"
  37 + issue.should have(1).note
  38 + issue.notes.first.downvote?.should be_false
  39 + issue.downvotes.should == 0
40 end 40 end
41 41
42 it "should recognize a single -1 note" do 42 it "should recognize a single -1 note" do
43 - merge_request.notes << create(:note, note: "-1 This is bad")  
44 - merge_request.downvotes.should == 1 43 + add_note "-1 This is bad"
  44 + issue.downvotes.should == 1
45 end 45 end
46 46
47 it "should recognize multiple -1 notes" do 47 it "should recognize multiple -1 notes" do
48 - merge_request.notes << create(:note, note: "-1 This is bad")  
49 - merge_request.notes << create(:note, note: "-1 Away with this")  
50 - merge_request.downvotes.should == 2 48 + add_note "-1 This is bad"
  49 + add_note "-1 Away with this"
  50 + issue.downvotes.should == 2
51 end 51 end
52 end 52 end
53 53
54 describe "#votes_count" do 54 describe "#votes_count" do
55 it "with no notes has a 0/0 score" do 55 it "with no notes has a 0/0 score" do
56 - merge_request.votes_count.should == 0 56 + issue.votes_count.should == 0
57 end 57 end
58 58
59 it "should recognize non notes" do 59 it "should recognize non notes" do
60 - merge_request.notes << create(:note, note: "No +1 here")  
61 - merge_request.should have(1).note  
62 - merge_request.votes_count.should == 0 60 + add_note "No +1 here"
  61 + issue.should have(1).note
  62 + issue.votes_count.should == 0
63 end 63 end
64 64
65 it "should recognize a single +1 note" do 65 it "should recognize a single +1 note" do
66 - merge_request.notes << create(:note, note: "+1 This is awesome")  
67 - merge_request.votes_count.should == 1 66 + add_note "+1 This is awesome"
  67 + issue.votes_count.should == 1
68 end 68 end
69 69
70 it "should recognize a single -1 note" do 70 it "should recognize a single -1 note" do
71 - merge_request.notes << create(:note, note: "-1 This is bad")  
72 - merge_request.votes_count.should == 1 71 + add_note "-1 This is bad"
  72 + issue.votes_count.should == 1
73 end 73 end
74 74
75 it "should recognize multiple notes" do 75 it "should recognize multiple notes" do
76 - merge_request.notes << create(:note, note: "+1 This is awesome")  
77 - merge_request.notes << create(:note, note: "-1 This is bad")  
78 - merge_request.notes << create(:note, note: "+1 I want this")  
79 - merge_request.votes_count.should == 3 76 + add_note "+1 This is awesome"
  77 + add_note "-1 This is bad"
  78 + add_note "+1 I want this"
  79 + issue.votes_count.should == 3
80 end 80 end
81 end 81 end
82 82
83 describe "#upvotes_in_percent" do 83 describe "#upvotes_in_percent" do
84 it "with no notes has a 0% score" do 84 it "with no notes has a 0% score" do
85 - merge_request.upvotes_in_percent.should == 0 85 + issue.upvotes_in_percent.should == 0
86 end 86 end
87 87
88 it "should count a single 1 note as 100%" do 88 it "should count a single 1 note as 100%" do
89 - merge_request.notes << create(:note, note: "+1 This is awesome")  
90 - merge_request.upvotes_in_percent.should == 100 89 + add_note "+1 This is awesome"
  90 + issue.upvotes_in_percent.should == 100
91 end 91 end
92 92
93 it "should count multiple +1 notes as 100%" do 93 it "should count multiple +1 notes as 100%" do
94 - merge_request.notes << create(:note, note: "+1 This is awesome")  
95 - merge_request.notes << create(:note, note: "+1 I want this")  
96 - merge_request.upvotes_in_percent.should == 100 94 + add_note "+1 This is awesome"
  95 + add_note "+1 I want this"
  96 + issue.upvotes_in_percent.should == 100
97 end 97 end
98 98
99 it "should count fractions for multiple +1 and -1 notes correctly" do 99 it "should count fractions for multiple +1 and -1 notes correctly" do
100 - merge_request.notes << create(:note, note: "+1 This is awesome")  
101 - merge_request.notes << create(:note, note: "+1 I want this")  
102 - merge_request.notes << create(:note, note: "-1 This is bad")  
103 - merge_request.notes << create(:note, note: "+1 me too")  
104 - merge_request.upvotes_in_percent.should == 75 100 + add_note "+1 This is awesome"
  101 + add_note "+1 I want this"
  102 + add_note "-1 This is bad"
  103 + add_note "+1 me too"
  104 + issue.upvotes_in_percent.should == 75
105 end 105 end
106 end 106 end
107 107
108 describe "#downvotes_in_percent" do 108 describe "#downvotes_in_percent" do
109 it "with no notes has a 0% score" do 109 it "with no notes has a 0% score" do
110 - merge_request.downvotes_in_percent.should == 0 110 + issue.downvotes_in_percent.should == 0
111 end 111 end
112 112
113 it "should count a single -1 note as 100%" do 113 it "should count a single -1 note as 100%" do
114 - merge_request.notes << create(:note, note: "-1 This is bad")  
115 - merge_request.downvotes_in_percent.should == 100 114 + add_note "-1 This is bad"
  115 + issue.downvotes_in_percent.should == 100
116 end 116 end
117 117
118 it "should count multiple -1 notes as 100%" do 118 it "should count multiple -1 notes as 100%" do
119 - merge_request.notes << create(:note, note: "-1 This is bad")  
120 - merge_request.notes << create(:note, note: "-1 Away with this")  
121 - merge_request.downvotes_in_percent.should == 100 119 + add_note "-1 This is bad"
  120 + add_note "-1 Away with this"
  121 + issue.downvotes_in_percent.should == 100
122 end 122 end
123 123
124 it "should count fractions for multiple +1 and -1 notes correctly" do 124 it "should count fractions for multiple +1 and -1 notes correctly" do
125 - merge_request.notes << create(:note, note: "+1 This is awesome")  
126 - merge_request.notes << create(:note, note: "+1 I want this")  
127 - merge_request.notes << create(:note, note: "-1 This is bad")  
128 - merge_request.notes << create(:note, note: "+1 me too")  
129 - merge_request.downvotes_in_percent.should == 25 125 + add_note "+1 This is awesome"
  126 + add_note "+1 I want this"
  127 + add_note "-1 This is bad"
  128 + add_note "+1 me too"
  129 + issue.downvotes_in_percent.should == 25
130 end 130 end
131 end 131 end
  132 +
  133 + def add_note(text)
  134 + issue.notes << create(:note, note: text, project: issue.project)
  135 + end
132 end 136 end
spec/models/project_spec.rb
@@ -39,7 +39,6 @@ describe Project do @@ -39,7 +39,6 @@ describe Project do
39 it { should have_many(:snippets).dependent(:destroy) } 39 it { should have_many(:snippets).dependent(:destroy) }
40 it { should have_many(:deploy_keys).dependent(:destroy) } 40 it { should have_many(:deploy_keys).dependent(:destroy) }
41 it { should have_many(:hooks).dependent(:destroy) } 41 it { should have_many(:hooks).dependent(:destroy) }
42 - it { should have_many(:wikis).dependent(:destroy) }  
43 it { should have_many(:protected_branches).dependent(:destroy) } 42 it { should have_many(:protected_branches).dependent(:destroy) }
44 end 43 end
45 44
@@ -97,6 +96,7 @@ describe Project do @@ -97,6 +96,7 @@ describe Project do
97 end 96 end
98 97
99 describe "last_activity methods" do 98 describe "last_activity methods" do
  99 + before { enable_observers }
100 let(:project) { create(:project) } 100 let(:project) { create(:project) }
101 let(:last_event) { double(created_at: Time.now) } 101 let(:last_event) { double(created_at: Time.now) }
102 102
spec/observers/issue_observer_spec.rb
@@ -4,11 +4,7 @@ describe IssueObserver do @@ -4,11 +4,7 @@ describe IssueObserver do
4 let(:some_user) { create :user } 4 let(:some_user) { create :user }
5 let(:assignee) { create :user } 5 let(:assignee) { create :user }
6 let(:author) { create :user } 6 let(:author) { create :user }
7 - let(:mock_issue) { double(:issue, id: 42, assignee: assignee, author: author) }  
8 - let(:assigned_issue) { create(:issue, assignee: assignee, author: author) }  
9 - let(:unassigned_issue) { create(:issue, author: author) }  
10 - let(:closed_assigned_issue) { create(:closed_issue, assignee: assignee, author: author) }  
11 - let(:closed_unassigned_issue) { create(:closed_issue, author: author) } 7 + let(:mock_issue) { create(:issue, assignee: assignee, author: author) }
12 8
13 9
14 before { subject.stub(:current_user).and_return(some_user) } 10 before { subject.stub(:current_user).and_return(some_user) }
@@ -18,15 +14,6 @@ describe IssueObserver do @@ -18,15 +14,6 @@ describe IssueObserver do
18 subject { IssueObserver.instance } 14 subject { IssueObserver.instance }
19 15
20 describe '#after_create' do 16 describe '#after_create' do
21 -  
22 - it 'is called when an issue is created' do  
23 - subject.should_receive(:after_create)  
24 -  
25 - Issue.observers.enable :issue_observer do  
26 - create(:issue, project: create(:project))  
27 - end  
28 - end  
29 -  
30 it 'trigger notification to send emails' do 17 it 'trigger notification to send emails' do
31 subject.should_receive(:notification) 18 subject.should_receive(:notification)
32 19
@@ -36,41 +23,28 @@ describe IssueObserver do @@ -36,41 +23,28 @@ describe IssueObserver do
36 23
37 context '#after_close' do 24 context '#after_close' do
38 context 'a status "closed"' do 25 context 'a status "closed"' do
  26 + before { mock_issue.stub(state: 'closed') }
  27 +
39 it 'note is created if the issue is being closed' do 28 it 'note is created if the issue is being closed' do
40 - Note.should_receive(:create_status_change_note).with(assigned_issue, some_user, 'closed') 29 + Note.should_receive(:create_status_change_note).with(mock_issue, some_user, 'closed')
41 30
42 - assigned_issue.close 31 + subject.after_close(mock_issue, nil)
43 end 32 end
44 33
45 it 'trigger notification to send emails' do 34 it 'trigger notification to send emails' do
46 - subject.should_receive(:notification)  
47 -  
48 - assigned_issue.close  
49 - end 35 + subject.notification.should_receive(:close_issue).with(mock_issue, some_user)
50 36
51 - it 'creates a note' do  
52 - Note.should_receive(:create_status_change_note).with(unassigned_issue, some_user, 'closed')  
53 - unassigned_issue.close 37 + subject.after_close(mock_issue, nil)
54 end 38 end
55 end 39 end
56 40
57 context 'a status "reopened"' do 41 context 'a status "reopened"' do
58 - it 'note is created if the issue is being reopened' do  
59 - Note.should_receive(:create_status_change_note).with(closed_assigned_issue, some_user, 'reopened')  
60 -  
61 - closed_assigned_issue.reopen  
62 - end  
63 -  
64 - it 'trigger notification to send emails' do  
65 - subject.should_receive(:notification)  
66 -  
67 - closed_assigned_issue.reopen  
68 - end 42 + before { mock_issue.stub(state: 'reopened') }
69 43
70 - it 'create a note' do  
71 - Note.should_receive(:create_status_change_note).with(closed_unassigned_issue, some_user, 'reopened') 44 + it 'note is created if the issue is being reopened' do
  45 + Note.should_receive(:create_status_change_note).with(mock_issue, some_user, 'reopened')
72 46
73 - closed_unassigned_issue.reopen 47 + subject.after_reopen(mock_issue, nil)
74 end 48 end
75 end 49 end
76 end 50 end
@@ -80,16 +54,6 @@ describe IssueObserver do @@ -80,16 +54,6 @@ describe IssueObserver do
80 mock_issue.stub(:is_being_reassigned?).and_return(false) 54 mock_issue.stub(:is_being_reassigned?).and_return(false)
81 end 55 end
82 56
83 - it 'is called when an issue is changed' do  
84 - changed = create(:issue, project: create(:project))  
85 - subject.should_receive(:after_update)  
86 -  
87 - Issue.observers.enable :issue_observer do  
88 - changed.description = 'I changed'  
89 - changed.save  
90 - end  
91 - end  
92 -  
93 context 'notification' do 57 context 'notification' do
94 it 'triggered if the issue is being reassigned' do 58 it 'triggered if the issue is being reassigned' do
95 mock_issue.should_receive(:is_being_reassigned?).and_return(true) 59 mock_issue.should_receive(:is_being_reassigned?).and_return(true)
spec/observers/merge_request_observer_spec.rb
@@ -16,12 +16,6 @@ describe MergeRequestObserver do @@ -16,12 +16,6 @@ describe MergeRequestObserver do
16 subject { MergeRequestObserver.instance } 16 subject { MergeRequestObserver.instance }
17 17
18 describe '#after_create' do 18 describe '#after_create' do
19 -  
20 - it 'is called when a merge request is created' do  
21 - subject.should_receive(:after_create)  
22 - create(:merge_request, project: create(:project))  
23 - end  
24 -  
25 it 'trigger notification service' do 19 it 'trigger notification service' do
26 subject.should_receive(:notification) 20 subject.should_receive(:notification)
27 subject.after_create(mr_mock) 21 subject.after_create(mr_mock)
spec/observers/user_observer_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe UserObserver do 3 describe UserObserver do
  4 + before(:each) { enable_observers }
4 subject { UserObserver.instance } 5 subject { UserObserver.instance }
5 before { subject.stub(notification: mock('NotificationService').as_null_object) } 6 before { subject.stub(notification: mock('NotificationService').as_null_object) }
6 7
spec/observers/users_project_observer_spec.rb
1 require 'spec_helper' 1 require 'spec_helper'
2 2
3 describe UsersProjectObserver do 3 describe UsersProjectObserver do
  4 + before(:each) { enable_observers }
  5 +
4 let(:user) { create(:user) } 6 let(:user) { create(:user) }
5 let(:project) { create(:project) } 7 let(:project) { create(:project) }
6 subject { UsersProjectObserver.instance } 8 subject { UsersProjectObserver.instance }
spec/requests/api/milestones_spec.rb
@@ -2,6 +2,7 @@ require &#39;spec_helper&#39; @@ -2,6 +2,7 @@ require &#39;spec_helper&#39;
2 2
3 describe Gitlab::API do 3 describe Gitlab::API do
4 include ApiHelpers 4 include ApiHelpers
  5 + before(:each) { enable_observers }
5 6
6 let(:user) { create(:user) } 7 let(:user) { create(:user) }
7 let!(:project) { create(:project, namespace: user.namespace ) } 8 let!(:project) { create(:project, namespace: user.namespace ) }
spec/requests/api/projects_spec.rb
@@ -2,6 +2,7 @@ require &#39;spec_helper&#39; @@ -2,6 +2,7 @@ require &#39;spec_helper&#39;
2 2
3 describe Gitlab::API do 3 describe Gitlab::API do
4 include ApiHelpers 4 include ApiHelpers
  5 + before(:each) { enable_observers }
5 6
6 let(:user) { create(:user) } 7 let(:user) { create(:user) }
7 let(:user2) { create(:user) } 8 let(:user2) { create(:user) }
spec/spec_helper.rb
@@ -41,13 +41,15 @@ Spork.prefork do @@ -41,13 +41,15 @@ Spork.prefork do
41 config.include FactoryGirl::Syntax::Methods 41 config.include FactoryGirl::Syntax::Methods
42 config.include Devise::TestHelpers, type: :controller 42 config.include Devise::TestHelpers, type: :controller
43 43
  44 + config.include TestEnv
  45 +
44 # If you're not using ActiveRecord, or you'd prefer not to run each of your 46 # If you're not using ActiveRecord, or you'd prefer not to run each of your
45 # examples within a transaction, remove the following line or assign false 47 # examples within a transaction, remove the following line or assign false
46 # instead of true. 48 # instead of true.
47 config.use_transactional_fixtures = false 49 config.use_transactional_fixtures = false
48 50
49 config.before do 51 config.before do
50 - TestEnv.init 52 + TestEnv.init(observers: false)
51 end 53 end
52 end 54 end
53 end 55 end
spec/support/test_env.rb
@@ -12,7 +12,15 @@ module TestEnv @@ -12,7 +12,15 @@ module TestEnv
12 # - add_key 12 # - add_key
13 # - remove_key 13 # - remove_key
14 # 14 #
15 - def init 15 + def init(opts = {})
  16 + # Disable observers to improve test speed
  17 + #
  18 + # You can enable it in whole test case where needed by next string:
  19 + #
  20 + # before(:each) { enable_observers }
  21 + #
  22 + disable_observers if opts[:observers] == false
  23 +
16 # Use tmp dir for FS manipulations 24 # Use tmp dir for FS manipulations
17 repos_path = Rails.root.join('tmp', 'test-git-base-path') 25 repos_path = Rails.root.join('tmp', 'test-git-base-path')
18 Gitlab.config.gitlab_shell.stub(repos_path: repos_path) 26 Gitlab.config.gitlab_shell.stub(repos_path: repos_path)
@@ -60,4 +68,12 @@ module TestEnv @@ -60,4 +68,12 @@ module TestEnv
60 command = "git init --quiet --bare #{path};" 68 command = "git init --quiet --bare #{path};"
61 system(command) 69 system(command)
62 end 70 end
  71 +
  72 + def enable_observers
  73 + ActiveRecord::Base.observers.enable(:all)
  74 + end
  75 +
  76 + def disable_observers
  77 + ActiveRecord::Base.observers.disable(:all)
  78 + end
63 end 79 end