Commit e9d52669f88e8f742ea46fe91cf94aeb71495dab

Authored by Heitor
Committed by Diego Camarinha
1 parent 88beabe7

Fixed unit tests for kalibro configuration and reading group

Signed off by: Pedro Scocco <pedroscocco@gmail.com>
spec/controllers/kalibro_configurations_controller_spec.rb
1 require 'rails_helper' 1 require 'rails_helper'
2 2
3 describe KalibroConfigurationsController, :type => :controller do 3 describe KalibroConfigurationsController, :type => :controller do
  4 +
  5 + def post_method(method)
  6 + unless method == :create
  7 + post method, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params, :attributes => attributes
  8 + else
  9 + post method, :kalibro_configuration => kalibro_configuration_params, :attributes => attributes
  10 + end
  11 + end
4 12
5 describe 'new' do 13 describe 'new' do
6 before :each do 14 before :each do
@@ -19,7 +27,8 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -19,7 +27,8 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
19 27
20 context 'with valid fields' do 28 context 'with valid fields' do
21 let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) } 29 let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration, :with_id) }
22 - let(:subject_params) { kalibro_configuration.to_hash } 30 + let(:kalibro_configuration_params) { kalibro_configuration.to_hash }
  31 + let(:attributes) { {public: "1"} }
23 32
24 before :each do 33 before :each do
25 KalibroConfiguration.any_instance.expects(:save).returns(true) 34 KalibroConfiguration.any_instance.expects(:save).returns(true)
@@ -27,7 +36,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -27,7 +36,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
27 36
28 context 'rendering the show' do 37 context 'rendering the show' do
29 before :each do 38 before :each do
30 - post :create, :kalibro_configuration => subject_params 39 + post_method :create
31 end 40 end
32 41
33 it 'should redirect to the show view' do 42 it 'should redirect to the show view' do
@@ -37,7 +46,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -37,7 +46,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
37 46
38 context 'without rendering the show view' do 47 context 'without rendering the show view' do
39 before :each do 48 before :each do
40 - post :create, :kalibro_configuration => subject_params 49 + post_method :create
41 end 50 end
42 51
43 it { is_expected.to respond_with(:redirect) } 52 it { is_expected.to respond_with(:redirect) }
@@ -195,8 +204,9 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -195,8 +204,9 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
195 end 204 end
196 205
197 describe 'update' do 206 describe 'update' do
198 - let(:kalibro_configuration) {FactoryGirl.build(:kalibro_configuration, :with_id)}  
199 - let(:kalibro_configuration_params) { kalibro_configuration.to_hash } 207 + let!(:kalibro_configuration) {FactoryGirl.build(:kalibro_configuration, :with_id)}
  208 + let!(:kalibro_configuration_params) { kalibro_configuration.to_hash }
  209 + let!(:attributes) { {public: "1"} }
200 210
201 context 'when the user is logged in' do 211 context 'when the user is logged in' do
202 before do 212 before do
@@ -216,11 +226,12 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -216,11 +226,12 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
216 before :each do 226 before :each do
217 KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration) 227 KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration)
218 KalibroConfiguration.any_instance.expects(:update).with(kalibro_configuration_params).returns(true) 228 KalibroConfiguration.any_instance.expects(:update).with(kalibro_configuration_params).returns(true)
  229 + kalibro_configuration.expects(:attributes).returns(kalibro_configuration_attribute)
219 end 230 end
220 231
221 context 'rendering the show' do 232 context 'rendering the show' do
222 before :each do 233 before :each do
223 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 234 + post_method :update
224 end 235 end
225 236
226 it 'should redirect to the show view' do 237 it 'should redirect to the show view' do
@@ -230,7 +241,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -230,7 +241,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
230 241
231 context 'without rendering the show view' do 242 context 'without rendering the show view' do
232 before :each do 243 before :each do
233 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 244 + post_method :update
234 end 245 end
235 246
236 it { is_expected.to respond_with(:redirect) } 247 it { is_expected.to respond_with(:redirect) }
@@ -242,7 +253,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -242,7 +253,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
242 KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration) 253 KalibroConfiguration.expects(:find).with(kalibro_configuration.id).returns(kalibro_configuration)
243 KalibroConfiguration.any_instance.expects(:update).with(kalibro_configuration_params).returns(false) 254 KalibroConfiguration.any_instance.expects(:update).with(kalibro_configuration_params).returns(false)
244 255
245 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 256 + post_method :update
246 end 257 end
247 258
248 it { is_expected.to render_template(:edit) } 259 it { is_expected.to render_template(:edit) }
@@ -251,7 +262,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -251,7 +262,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
251 262
252 context 'when the user does not own the kalibro_configuration' do 263 context 'when the user does not own the kalibro_configuration' do
253 before :each do 264 before :each do
254 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 265 + post_method :update
255 end 266 end
256 267
257 it { is_expected.to redirect_to kalibro_configurations_path(id: kalibro_configuration.id) } 268 it { is_expected.to redirect_to kalibro_configurations_path(id: kalibro_configuration.id) }
@@ -260,7 +271,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do @@ -260,7 +271,7 @@ describe KalibroConfigurationsController, :type =&gt; :controller do
260 271
261 context 'with no user logged in' do 272 context 'with no user logged in' do
262 before :each do 273 before :each do
263 - post :update, :id => kalibro_configuration.id, :kalibro_configuration => kalibro_configuration_params 274 + post_method :update
264 end 275 end
265 276
266 it { is_expected.to redirect_to new_user_session_path } 277 it { is_expected.to redirect_to new_user_session_path }
spec/controllers/reading_groups_controller_spec.rb
@@ -12,6 +12,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -12,6 +12,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
12 end 12 end
13 13
14 describe 'create' do 14 describe 'create' do
  15 + let(:attributes) { {public: "1"} }
15 before do 16 before do
16 sign_in FactoryGirl.create(:user) 17 sign_in FactoryGirl.create(:user)
17 end 18 end
@@ -26,7 +27,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -26,7 +27,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
26 27
27 context 'rendering the show' do 28 context 'rendering the show' do
28 before :each do 29 before :each do
29 - post :create, :reading_group => subject_params 30 + post :create, :reading_group => subject_params, :attributes => attributes
  31 +
30 end 32 end
31 33
32 it 'should redirect to the show view' do 34 it 'should redirect to the show view' do
@@ -36,7 +38,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -36,7 +38,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
36 38
37 context 'without rendering the show view' do 39 context 'without rendering the show view' do
38 before :each do 40 before :each do
39 - post :create, :reading_group => subject_params 41 + post :create, :reading_group => subject_params, :attributes => attributes
  42 +
40 end 43 end
41 44
42 it { is_expected.to respond_with(:redirect) } 45 it { is_expected.to respond_with(:redirect) }
@@ -51,7 +54,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -51,7 +54,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
51 ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject) 54 ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject)
52 ReadingGroup.any_instance.expects(:save).returns(false) 55 ReadingGroup.any_instance.expects(:save).returns(false)
53 56
54 - post :create, :reading_group => @subject_params 57 + post :create, :reading_group => @subject_params, :attributes => attributes
  58 +
55 end 59 end
56 60
57 it { is_expected.to render_template(:new) } 61 it { is_expected.to render_template(:new) }
@@ -194,6 +198,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -194,6 +198,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
194 before do 198 before do
195 @subject = FactoryGirl.build(:reading_group, :with_id) 199 @subject = FactoryGirl.build(:reading_group, :with_id)
196 @subject_params = @subject.to_hash 200 @subject_params = @subject.to_hash
  201 + @ownership = FactoryGirl.build(:reading_group_attributes, reading_group_id: @subject.id)
197 end 202 end
198 203
199 context 'when the user is logged in' do 204 context 'when the user is logged in' do
@@ -203,7 +208,6 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -203,7 +208,6 @@ describe ReadingGroupsController, :type =&gt; :controller do
203 208
204 context 'when user owns the reading group' do 209 context 'when user owns the reading group' do
205 before do 210 before do
206 - @ownership = FactoryGirl.build(:reading_group_attributes)  
207 @ownerships = [] 211 @ownerships = []
208 212
209 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) 213 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
@@ -212,13 +216,14 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -212,13 +216,14 @@ describe ReadingGroupsController, :type =&gt; :controller do
212 216
213 context 'with valid fields' do 217 context 'with valid fields' do
214 before :each do 218 before :each do
  219 + @subject.expects(:attributes).returns(@ownership)
215 ReadingGroup.expects(:find).with(@subject.id).returns(@subject) 220 ReadingGroup.expects(:find).with(@subject.id).returns(@subject)
216 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true) 221 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true)
217 end 222 end
218 223
219 context 'rendering the show' do 224 context 'rendering the show' do
220 before :each do 225 before :each do
221 - post :update, :id => @subject.id, :reading_group => @subject_params 226 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
222 end 227 end
223 228
224 it 'should redirect to the show view' do 229 it 'should redirect to the show view' do
@@ -228,7 +233,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -228,7 +233,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
228 233
229 context 'without rendering the show view' do 234 context 'without rendering the show view' do
230 before :each do 235 before :each do
231 - post :update, :id => @subject.id, :reading_group => @subject_params 236 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  237 +
232 end 238 end
233 239
234 it { is_expected.to respond_with(:redirect) } 240 it { is_expected.to respond_with(:redirect) }
@@ -240,7 +246,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -240,7 +246,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
240 ReadingGroup.expects(:find).with(@subject.id).returns(@subject) 246 ReadingGroup.expects(:find).with(@subject.id).returns(@subject)
241 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false) 247 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false)
242 248
243 - post :update, :id => @subject.id, :reading_group => @subject_params 249 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  250 +
244 end 251 end
245 252
246 it { is_expected.to render_template(:edit) } 253 it { is_expected.to render_template(:edit) }
@@ -249,7 +256,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -249,7 +256,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
249 256
250 context 'when the user does not own the reading group' do 257 context 'when the user does not own the reading group' do
251 before :each do 258 before :each do
252 - post :update, :id => @subject.id, :reading_group => @subject_params 259 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  260 +
253 end 261 end
254 262
255 it { is_expected.to redirect_to reading_group_path } 263 it { is_expected.to redirect_to reading_group_path }
@@ -258,7 +266,8 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -258,7 +266,8 @@ describe ReadingGroupsController, :type =&gt; :controller do
258 266
259 context 'with no user logged in' do 267 context 'with no user logged in' do
260 before :each do 268 before :each do
261 - post :update, :id => @subject.id, :reading_group => @subject_params 269 + post :update, :id => @subject.id, :reading_group => @subject_params, :attributes => {public: @ownership.public}
  270 +
262 end 271 end
263 272
264 it { is_expected.to redirect_to new_user_session_path } 273 it { is_expected.to redirect_to new_user_session_path }