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