Commit 201cc586167f980011a4108ff79e07b7c071700f
1 parent
3ac840f8
Exists in
colab
and in
4 other branches
Improved ProjectsController unit test coverage
before_action branch cverage
Showing
1 changed file
with
85 additions
and
45 deletions
Show diff stats
spec/controllers/projects_controller_spec.rb
... | ... | @@ -73,31 +73,59 @@ describe ProjectsController do |
73 | 73 | end |
74 | 74 | |
75 | 75 | describe 'destroy' do |
76 | - before :each do | |
77 | - sign_in FactoryGirl.create(:user) | |
78 | - | |
76 | + before do | |
79 | 77 | @subject = FactoryGirl.build(:project) |
80 | - @subject.expects(:destroy) | |
78 | + end | |
81 | 79 | |
82 | - @ownership = FactoryGirl.build(:project_ownership) | |
83 | - @ownership.expects(:destroy) | |
84 | - @ownerships = [] | |
80 | + context 'with an User logged in' do | |
81 | + before do | |
82 | + sign_in FactoryGirl.create(:user) | |
83 | + @ownership = FactoryGirl.build(:project_ownership) | |
84 | + @ownerships = [] | |
85 | + | |
86 | + end | |
85 | 87 | |
86 | - #Those two mocks looks the same but they are necessary since params[:id] is a String and @project.id is an Integer :( | |
87 | - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
88 | - @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership) | |
88 | + context 'when the user owns the project' do | |
89 | + before :each do | |
90 | + @ownership.expects(:destroy) | |
91 | + @subject.expects(:destroy) | |
89 | 92 | |
90 | - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
93 | + #Those two mocks looks the same but they are necessary since params[:id] is a String and @project.id is an Integer :( | |
94 | + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
95 | + @ownerships.expects(:find_by_project_id).with(@subject.id).returns(@ownership) | |
91 | 96 | |
92 | - Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
93 | - delete :destroy, :id => @subject.id | |
94 | - end | |
97 | + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
98 | + | |
99 | + Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
100 | + delete :destroy, :id => @subject.id | |
101 | + end | |
102 | + | |
103 | + it 'should redirect to the projects page' do | |
104 | + response.should redirect_to projects_url | |
105 | + end | |
106 | + | |
107 | + it { should respond_with(:redirect) } | |
108 | + end | |
95 | 109 | |
96 | - it 'should redirect to the projects page' do | |
97 | - response.should redirect_to projects_url | |
110 | + context "when the user doesn't own the project" do | |
111 | + before :each do | |
112 | + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil) | |
113 | + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
114 | + | |
115 | + delete :destroy, :id => @subject.id | |
116 | + end | |
117 | + | |
118 | + it { should redirect_to(projects_path) } | |
119 | + end | |
98 | 120 | end |
99 | 121 | |
100 | - it { should respond_with(:redirect) } | |
122 | + context 'with no User logged in' do | |
123 | + before :each do | |
124 | + delete :destroy, :id => @subject.id | |
125 | + end | |
126 | + | |
127 | + it { should redirect_to new_user_session_path } | |
128 | + end | |
101 | 129 | end |
102 | 130 | |
103 | 131 | describe 'index' do |
... | ... | @@ -112,47 +140,60 @@ describe ProjectsController do |
112 | 140 | |
113 | 141 | describe 'edit' do |
114 | 142 | before do |
115 | - @user = FactoryGirl.create(:user) | |
116 | 143 | @subject = FactoryGirl.build(:project) |
117 | - @ownership = FactoryGirl.build(:project_ownership) | |
118 | - @ownerships = [] | |
119 | - | |
120 | - User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
121 | - | |
122 | - sign_in @user | |
123 | 144 | end |
124 | 145 | |
125 | - context 'when the user owns the project' do | |
126 | - before :each do | |
127 | - Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
128 | - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
129 | - | |
130 | - get :edit, :id => @subject.id | |
146 | + context 'with an User logged in' do | |
147 | + before do | |
148 | + @user = FactoryGirl.create(:user) | |
149 | + @ownership = FactoryGirl.build(:project_ownership) | |
150 | + @ownerships = [] | |
151 | + | |
152 | + User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) | |
153 | + | |
154 | + sign_in @user | |
131 | 155 | end |
132 | 156 | |
133 | - it { should render_template(:edit) } | |
157 | + context 'when the user owns the project' do | |
158 | + before :each do | |
159 | + Project.expects(:find).with(@subject.id.to_s).returns(@subject) | |
160 | + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) | |
161 | + | |
162 | + get :edit, :id => @subject.id | |
163 | + end | |
134 | 164 | |
135 | - it 'should assign to @project the @subject' do | |
136 | - assigns(:project).should eq(@subject) | |
165 | + it { should render_template(:edit) } | |
166 | + | |
167 | + it 'should assign to @project the @subject' do | |
168 | + assigns(:project).should eq(@subject) | |
169 | + end | |
137 | 170 | end |
138 | - end | |
139 | 171 | |
140 | - context 'when the user does not own the project' do | |
141 | - before do | |
142 | - @subject = FactoryGirl.build(:another_project) | |
143 | - @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil) | |
172 | + context 'when the user does not own the project' do | |
173 | + before do | |
174 | + @subject = FactoryGirl.build(:another_project) | |
175 | + @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(nil) | |
144 | 176 | |
145 | - get :edit, :id => @subject.id | |
146 | - end | |
177 | + get :edit, :id => @subject.id | |
178 | + end | |
147 | 179 | |
148 | - it { should redirect_to(projects_path) } | |
180 | + it { should redirect_to(projects_path) } | |
149 | 181 | |
150 | - it 'should set the flash' do | |
151 | - pending("This ShouldaMatcher test is not compatible yet with Rails 4") do | |
152 | - should set_the_flash[:notice].to("You shall not edit projects that aren't yours.") | |
182 | + it 'should set the flash' do | |
183 | + pending("This ShouldaMatcher test is not compatible yet with Rails 4") do | |
184 | + should set_the_flash[:notice].to("You shall not edit projects that aren't yours.") | |
185 | + end | |
153 | 186 | end |
154 | 187 | end |
155 | 188 | end |
189 | + | |
190 | + context 'with no user logged in' do | |
191 | + before :each do | |
192 | + get :edit, :id => @subject.id | |
193 | + end | |
194 | + | |
195 | + it { should redirect_to new_user_session_path } | |
196 | + end | |
156 | 197 | end |
157 | 198 | |
158 | 199 | describe 'update' do |
... | ... | @@ -231,5 +272,4 @@ describe ProjectsController do |
231 | 272 | it { should redirect_to new_user_session_path } |
232 | 273 | end |
233 | 274 | end |
234 | - | |
235 | 275 | end | ... | ... |