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