Commit f3027748b3180d24a30822dfe7cc506e79e444d7

Authored by Heitor
Committed by Rafael Manzo
1 parent 63e8cd70

Finished tests for reading groups controller

Signed off by: Daniel Alves <danpaulalves@gmail.com>
Signed off by: Diego Araújo <diegoamc90@gmail.com>
Showing 1 changed file with 160 additions and 166 deletions   Show diff stats
spec/controllers/reading_groups_controller_spec.rb
1 1 require 'rails_helper'
2 2  
3 3 describe ReadingGroupsController, :type => :controller do
4   - pending 'waiting for kalibro configurations integration' do
5   - describe 'new' do
6   - before :each do
7   - sign_in FactoryGirl.create(:user)
8   - get :new
9   - end
10   -
11   - it { is_expected.to respond_with(:success) }
12   - it { is_expected.to render_template(:new) }
  4 + describe 'new' do
  5 + before :each do
  6 + sign_in FactoryGirl.create(:user)
  7 + get :new
13 8 end
14 9  
15   - describe 'create' do
16   - before do
17   - sign_in FactoryGirl.create(:user)
18   - end
19   -
20   - context 'with valid fields' do
21   - let(:reading_group) { FactoryGirl.build(:reading_group) }
22   - let(:subject_params) { Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
  10 + it { is_expected.to respond_with(:success) }
  11 + it { is_expected.to render_template(:new) }
  12 + end
23 13  
24   - before :each do
25   - ReadingGroup.any_instance.expects(:save).returns(true)
26   - end
  14 + describe 'create' do
  15 + before do
  16 + sign_in FactoryGirl.create(:user)
  17 + end
27 18  
28   - context 'rendering the show' do
29   - before :each do
30   - ReadingGroup.expects(:exists?).returns(true)
  19 + context 'with valid fields' do
  20 + let(:reading_group) { FactoryGirl.build(:reading_group) }
  21 + let(:subject_params) { Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
31 22  
32   - post :create, :reading_group => subject_params
33   - end
  23 + before :each do
  24 + ReadingGroup.any_instance.expects(:save).returns(true)
  25 + end
34 26  
35   - it 'should redirect to the show view' do
36   - expect(response).to redirect_to reading_group_path(reading_group)
37   - end
  27 + context 'rendering the show' do
  28 + before :each do
  29 + post :create, :reading_group => subject_params
38 30 end
39 31  
40   - context 'without rendering the show view' do
41   - before :each do
42   - post :create, :reading_group => subject_params
43   - end
44   -
45   - it { is_expected.to respond_with(:redirect) }
  32 + it 'should redirect to the show view' do
  33 + expect(response).to redirect_to reading_group_path(reading_group.id)
46 34 end
47 35 end
48 36  
49   - context 'with an invalid field' do
  37 + context 'without rendering the show view' do
50 38 before :each do
51   - @subject = FactoryGirl.build(:reading_group)
52   - @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
53   -
54   - ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject)
55   - ReadingGroup.any_instance.expects(:save).returns(false)
56   -
57   - post :create, :reading_group => @subject_params
  39 + post :create, :reading_group => subject_params
58 40 end
59 41  
60   - it { is_expected.to render_template(:new) }
  42 + it { is_expected.to respond_with(:redirect) }
61 43 end
62 44 end
63 45  
64   - describe 'show' do
65   - let!(:reading_group) { FactoryGirl.build(:reading_group) }
66   - let(:reading) { FactoryGirl.build(:reading) }
  46 + context 'with an invalid field' do
67 47 before :each do
68   - subject.expects(:find_resource).with(ReadingGroup, reading_group.id).returns(reading_group)
69   - get :show, :id => reading_group.id
70   - end
  48 + @subject = FactoryGirl.build(:reading_group)
  49 + @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
71 50  
72   - it { is_expected.to render_template(:show) }
73   - end
  51 + ReadingGroup.expects(:new).at_least_once.with(@subject_params).returns(@subject)
  52 + ReadingGroup.any_instance.expects(:save).returns(false)
74 53  
75   - describe 'destroy' do
76   - before do
77   - @subject = FactoryGirl.build(:reading_group)
  54 + post :create, :reading_group => @subject_params
78 55 end
79 56  
80   - context 'with an User logged in' do
81   - before do
82   - sign_in FactoryGirl.create(:user)
83   - @ownership = FactoryGirl.build(:reading_group_ownership)
84   - @ownerships = []
  57 + it { is_expected.to render_template(:new) }
  58 + end
  59 + end
85 60  
86   - end
  61 + describe 'show' do
  62 + let!(:reading_group) { FactoryGirl.build(:reading_group) }
  63 + let(:reading) { FactoryGirl.build(:reading) }
  64 + before :each do
  65 + subject.expects(:find_resource).with(ReadingGroup, reading_group.id).returns(reading_group)
  66 + get :show, :id => reading_group.id
  67 + end
87 68  
88   - context 'when the user owns the reading group' do
89   - before :each do
90   - @ownership.expects(:destroy)
91   - @subject.expects(:destroy)
  69 + it { is_expected.to render_template(:show) }
  70 + end
92 71  
93   - #Those two mocks looks the same but they are necessary since params[:id] is a String and @ReadingGroup.id is an Integer :(
94   - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
95   - @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership)
  72 + describe 'destroy' do
  73 + before do
  74 + @subject = FactoryGirl.build(:reading_group)
  75 + end
96 76  
97   - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
  77 + context 'with an User logged in' do
  78 + before do
  79 + sign_in FactoryGirl.create(:user)
  80 + @ownership = FactoryGirl.build(:reading_group_ownership)
  81 + @ownerships = []
98 82  
99   - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
100   - delete :destroy, :id => @subject.id
101   - end
  83 + end
102 84  
103   - it 'should redirect to the reading groups page' do
104   - expect(response).to redirect_to reading_groups_url
105   - end
  85 + context 'when the user owns the reading group' do
  86 + before :each do
  87 + @ownership.expects(:destroy)
  88 + @subject.expects(:destroy)
106 89  
107   - it { is_expected.to respond_with(:redirect) }
108   - end
  90 + #Those two mocks looks the same but they are necessary since params[:id] is a String and @ReadingGroup.id is an Integer :(
  91 + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
  92 + @ownerships.expects(:find_by_reading_group_id).with(@subject.id).returns(@ownership)
109 93  
110   - context "when the user doesn't own the reading group" do
111   - before :each do
112   - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil)
113   - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
  94 + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
114 95  
115   - delete :destroy, :id => @subject.id
116   - end
  96 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
  97 + delete :destroy, :id => @subject.id
  98 + end
117 99  
118   - it { is_expected.to redirect_to(reading_group_path) }
  100 + it 'should redirect to the reading groups page' do
  101 + expect(response).to redirect_to reading_groups_url
119 102 end
  103 +
  104 + it { is_expected.to respond_with(:redirect) }
120 105 end
121 106  
122   - context 'with no User logged in' do
  107 + context "when the user doesn't own the reading group" do
123 108 before :each do
  109 + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil)
  110 + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
  111 +
124 112 delete :destroy, :id => @subject.id
125 113 end
126 114  
127   - it { is_expected.to redirect_to new_user_session_path }
  115 + it { is_expected.to redirect_to(reading_group_path) }
128 116 end
129 117 end
130 118  
131   - describe 'index' do
  119 + context 'with no User logged in' do
132 120 before :each do
133   - @subject = FactoryGirl.build(:reading_group)
134   - ReadingGroup.expects(:all).returns([@subject])
135   - get :index
  121 + delete :destroy, :id => @subject.id
136 122 end
137 123  
138   - it { is_expected.to render_template(:index) }
  124 + it { is_expected.to redirect_to new_user_session_path }
139 125 end
  126 + end
140 127  
141   - describe 'edit' do
142   - before do
143   - @subject = FactoryGirl.build(:reading_group)
144   - end
  128 + describe 'index' do
  129 + before :each do
  130 + @subject = FactoryGirl.build(:reading_group)
  131 + ReadingGroup.expects(:all).returns([@subject])
  132 + get :index
  133 + end
145 134  
146   - context 'with an User logged in' do
147   - before do
148   - @user = FactoryGirl.create(:user)
149   - @ownership = FactoryGirl.build(:reading_group_ownership)
150   - @ownerships = []
  135 + it { is_expected.to render_template(:index) }
  136 + end
151 137  
152   - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
  138 + describe 'edit' do
  139 + before do
  140 + @subject = FactoryGirl.build(:reading_group)
  141 + end
153 142  
154   - sign_in @user
155   - end
  143 + context 'with an User logged in' do
  144 + before do
  145 + @user = FactoryGirl.create(:user)
  146 + @ownership = FactoryGirl.build(:reading_group_ownership)
  147 + @ownerships = []
156 148  
157   - context 'when the user owns the reading group' do
158   - before :each do
159   - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
160   - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
  149 + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
161 150  
162   - get :edit, :id => @subject.id
163   - end
  151 + sign_in @user
  152 + end
164 153  
165   - it { is_expected.to render_template(:edit) }
  154 + context 'when the user owns the reading group' do
  155 + before :each do
  156 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
  157 + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
166 158  
167   - it 'should assign to @reading group the @subject' do
168   - expect(assigns(:reading_group)).to eq(@subject)
169   - end
  159 + get :edit, :id => @subject.id
170 160 end
171 161  
172   - context 'when the user does not own the reading group' do
173   - before do
174   - @subject = FactoryGirl.build(:another_reading_group)
175   - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil)
176   -
177   - get :edit, :id => @subject.id
178   - end
  162 + it { is_expected.to render_template(:edit) }
179 163  
180   - it { is_expected.to redirect_to(reading_group_path) }
181   - it { is_expected.to set_the_flash[:notice].to("You're not allowed to do this operation") }
  164 + it 'should assign to @reading group the @subject' do
  165 + expect(assigns(:reading_group)).to eq(@subject)
182 166 end
183 167 end
184 168  
185   - context 'with no user logged in' do
186   - before :each do
  169 + context 'when the user does not own the reading group' do
  170 + before do
  171 + @subject = FactoryGirl.build(:another_reading_group)
  172 + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(nil)
  173 +
187 174 get :edit, :id => @subject.id
188 175 end
189 176  
190   - it { is_expected.to redirect_to new_user_session_path }
  177 + it { is_expected.to redirect_to(reading_group_path) }
  178 + it { is_expected.to set_the_flash[:notice].to("You're not allowed to do this operation") }
  179 + end
  180 + end
  181 +
  182 + context 'with no user logged in' do
  183 + before :each do
  184 + get :edit, :id => @subject.id
191 185 end
  186 +
  187 + it { is_expected.to redirect_to new_user_session_path }
  188 + end
  189 + end
  190 +
  191 + describe 'update' do
  192 + before do
  193 + @subject = FactoryGirl.build(:reading_group)
  194 + @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
192 195 end
193 196  
194   - describe 'update' do
  197 + context 'when the user is logged in' do
195 198 before do
196   - @subject = FactoryGirl.build(:reading_group)
197   - @subject_params = Hash[FactoryGirl.attributes_for(:reading_group).map { |k,v| [k.to_s, v.to_s] }] #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers
  199 + sign_in FactoryGirl.create(:user)
198 200 end
199 201  
200   - context 'when the user is logged in' do
  202 + context 'when user owns the reading group' do
201 203 before do
202   - sign_in FactoryGirl.create(:user)
203   - end
  204 + @ownership = FactoryGirl.build(:reading_group_ownership)
  205 + @ownerships = []
204 206  
205   - context 'when user owns the reading group' do
206   - before do
207   - @ownership = FactoryGirl.build(:reading_group_ownership)
208   - @ownerships = []
  207 + @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
  208 + User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
  209 + end
209 210  
210   - @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
211   - User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
  211 + context 'with valid fields' do
  212 + before :each do
  213 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
  214 + ReadingGroup.any_instance.expects(:save).with(@subject_params).returns(true)
212 215 end
213 216  
214   - context 'with valid fields' do
  217 + context 'rendering the show' do
215 218 before :each do
216   - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
217   - ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true)
218   - end
219   -
220   - context 'rendering the show' do
221   - before :each do
222   - ReadingGroup.expects(:exists?).returns(true)
223   -
224   - post :update, :id => @subject.id, :reading_group => @subject_params
225   - end
226   -
227   - it 'should redirect to the show view' do
228   - expect(response).to redirect_to reading_group_path(@subject)
229   - end
  219 + post :update, :id => @subject.id, :reading_group => @subject_params
230 220 end
231 221  
232   - context 'without rendering the show view' do
233   - before :each do
234   - post :update, :id => @subject.id, :reading_group => @subject_params
235   - end
236   -
237   - it { is_expected.to respond_with(:redirect) }
  222 + it 'should redirect to the show view' do
  223 + expect(response).to redirect_to reading_group_path(@subject.id)
238 224 end
239 225 end
240 226  
241   - context 'with an invalid field' do
  227 + context 'without rendering the show view' do
242 228 before :each do
243   - subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
244   - ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false)
245   -
246 229 post :update, :id => @subject.id, :reading_group => @subject_params
247 230 end
248 231  
249   - it { is_expected.to render_template(:edit) }
  232 + it { is_expected.to respond_with(:redirect) }
250 233 end
251 234 end
252 235  
253   - context 'when the user does not own the reading group' do
  236 + context 'with an invalid field' do
254 237 before :each do
  238 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
  239 + ReadingGroup.any_instance.expects(:save).with(@subject_params).returns(false)
  240 +
255 241 post :update, :id => @subject.id, :reading_group => @subject_params
256 242 end
257 243  
258   - it { is_expected.to redirect_to reading_group_path }
  244 + it { is_expected.to render_template(:edit) }
259 245 end
260 246 end
261 247  
262   - context 'with no user logged in' do
  248 + context 'when the user does not own the reading group' do
263 249 before :each do
264 250 post :update, :id => @subject.id, :reading_group => @subject_params
265 251 end
266 252  
267   - it { is_expected.to redirect_to new_user_session_path }
  253 + it { is_expected.to redirect_to reading_group_path }
268 254 end
269 255 end
  256 +
  257 + context 'with no user logged in' do
  258 + before :each do
  259 + post :update, :id => @subject.id, :reading_group => @subject_params
  260 + end
  261 +
  262 + it { is_expected.to redirect_to new_user_session_path }
  263 + end
270 264 end
271 265 end
... ...