/
users_controller_spec.rb
277 lines (215 loc) · 7.51 KB
/
users_controller_spec.rb
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
270
271
272
273
274
275
276
277
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), '../app'))
describe UsersController, "#route_for" do
it "should map { :controller => 'users', :action => 'index' } to /users" do
route_for(:controller => "users", :action => "index").should == "/users"
end
it "should map { :controller => 'users', :action => 'new' } to /users/new" do
route_for(:controller => "users", :action => "new").should == "/users/new"
end
it "should map { :controller => 'users', :action => 'show', :id => 'dave' } to /users/dave" do
route_for(:controller => "users", :action => "show", :id => 'dave').should == "/users/dave"
end
it "should map { :controller => 'users', :action => 'edit', :id => 'dave' } to /users/dave/edit" do
route_for(:controller => "users", :action => "edit", :id => 'dave').should == "/users/dave/edit"
end
it "should map { :controller => 'users', :action => 'update', :id => 'dave'} to /users/dave" do
route_for(:controller => "users", :action => "update", :id => 'dave').should == "/users/dave"
end
it "should map { :controller => 'users', :action => 'destroy', :id => 'dave'} to /users/dave" do
route_for(:controller => "users", :action => "destroy", :id => 'dave').should == "/users/dave"
end
end
describe UsersController, "#params_from" do
it "should generate params { :controller => 'users', action => 'index' } from GET /users" do
params_from(:get, "/users").should == {:controller => "users", :action => "index"}
end
it "should generate params { :controller => 'users', action => 'new' } from GET /users/new" do
params_from(:get, "/users/new").should == {:controller => "users", :action => "new"}
end
it "should generate params { :controller => 'users', action => 'create' } from POST /users" do
params_from(:post, "/users").should == {:controller => "users", :action => "create"}
end
it "should generate params { :controller => 'users', action => 'show', id => '1' } from GET /users/dave" do
params_from(:get, "/users/dave").should == {:controller => "users", :action => "show", :id => "dave"}
end
it "should generate params { :controller => 'users', action => 'edit', id => '1' } from GET /users/dave;edit" do
params_from(:get, "/users/dave/edit").should == {:controller => "users", :action => "edit", :id => "dave"}
end
it "should generate params { :controller => 'users', action => 'update', id => '1' } from PUT /users/dave" do
params_from(:put, "/users/dave").should == {:controller => "users", :action => "update", :id => "dave"}
end
it "should generate params { :controller => 'users', action => 'destroy', id => '1' } from DELETE /users/dave" do
params_from(:delete, "/users/dave").should == {:controller => "users", :action => "destroy", :id => "dave"}
end
end
describe UsersController, "handling GET /users" do
before do
@user = mock_model(User)
User.stub!(:find).and_return([@user])
end
def do_get
get :index
end
it "should be successful" do
do_get
response.should be_success
end
it "should render index template" do
do_get
response.should render_template('index')
end
it "should find all users" do
User.should_receive(:find).with(:all).and_return([@user])
do_get
end
it "should assign the found users for the view" do
do_get
assigns[:users].should == [@user]
end
end
describe UsersController, "handling GET /users.xml" do
before do
@user = mock_model(User, :to_xml => "XML")
User.stub!(:find).and_return(@user)
end
def do_get
@request.env["HTTP_ACCEPT"] = "application/xml"
get :index
end
it "should be successful" do
do_get
response.should be_success
end
it "should find all users" do
User.should_receive(:find).with(:all).and_return([@user])
do_get
end
it "should render the found users as xml" do
@user.should_receive(:to_xml).and_return("XML")
do_get
response.body.should == "XML"
end
end
describe UsersController, "handling GET /users/dave" do
before do
@user = mock_model(User)
User.stub!(:find_by_login).and_return(@user)
end
def do_get
get :show, :id => "dave"
end
it "should be successful" do
do_get
response.should be_success
end
it "should render show template" do
do_get
response.should render_template('show')
end
it "should find the user requested" do
User.should_receive(:find_by_login).with("dave").and_return(@user)
do_get
end
it "should assign the found user for the view" do
do_get
assigns[:user].should equal(@user)
end
end
describe UsersController, "handling GET /users/dave.xml" do
before do
@user = mock_model(User, :to_xml => "XML")
User.stub!(:find_by_login).and_return(@user)
end
def do_get
@request.env["HTTP_ACCEPT"] = "application/xml"
get :show, :id => "dave"
end
it "should be successful" do
do_get
response.should be_success
end
it "should find the user requested" do
User.should_receive(:find_by_login).with("dave").and_return(@user)
do_get
end
it "should render the found user as xml" do
@user.should_receive(:to_xml).and_return("XML")
do_get
response.body.should == "XML"
end
end
describe UsersController, "handling GET /users/new" do
it "should be unknown action" do
lambda{ get :new }.should raise_error(ActionController::UnknownAction)
end
end
describe UsersController, "handling GET /users/dave/edit" do
before do
@user = mock_model(User)
User.stub!(:find_by_login).and_return(@user)
end
def do_get
get :edit, :id => "dave"
end
it "should be successful" do
do_get
response.should be_success
end
it "should render edit template" do
do_get
response.should render_template('edit')
end
it "should find the user requested" do
User.should_receive(:find_by_login).and_return(@user)
do_get
end
it "should assign the found User for the view" do
do_get
assigns[:user].should equal(@user)
end
end
describe UsersController, "handling POST /users" do
it "should be unknown action" do
lambda{ post :create }.should raise_error(ActionController::UnknownAction)
end
end
describe UsersController, "handling PUT /users/dave" do
before do
@user = mock_model(User, :to_param => "dave")
User.stub!(:find_by_login).and_return(@user)
end
def put_with_successful_update
@user.should_receive(:update_attributes).and_return(true)
put :update, :id => "dave"
end
def put_with_failed_update
@user.should_receive(:update_attributes).and_return(false)
put :update, :id => "dave"
end
it "should find the user requested" do
User.should_receive(:find_by_login).with("dave").and_return(@user)
put_with_successful_update
end
it "should update the found user" do
put_with_successful_update
assigns(:user).should equal(@user)
end
it "should assign the found user for the view" do
put_with_successful_update
assigns(:user).should equal(@user)
end
it "should redirect to the user on successful update" do
put_with_successful_update
response.should redirect_to(user_url("dave"))
end
it "should re-render 'edit' on failed update" do
put_with_failed_update
response.should render_template('edit')
end
end
describe UsersController, "handling DELETE /users/dave" do
it "should be unknown action" do
lambda{ delete :destroy, :id => "dave" }.should raise_error(ActionController::UnknownAction)
end
end