changelog shortlog tags changeset manifest revisions annotate raw

spec/controllers/products_controller_spec.rb

changeset 18: b97ed3573af2
author: moriq <moriq@moriq.com>
date: Mon Mar 10 10:39:23 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: generate rspec_scaffold product.
1require File.dirname(__FILE__) + '/../spec_helper'
2
3describe ProductsController do
4 describe "handling GET /products" do
5
6 before(:each) do
7 @product = mock_model(Product)
8 Product.stub!(:find).and_return([@product])
9 end
10
11 def do_get
12 get :index
13 end
14
15 it "should be successful" do
16 do_get
17 response.should be_success
18 end
19
20 it "should render index template" do
21 do_get
22 response.should render_template('index')
23 end
24
25 it "should find all products" do
26 Product.should_receive(:find).with(:all).and_return([@product])
27 do_get
28 end
29
30 it "should assign the found products for the view" do
31 do_get
32 assigns[:products].should == [@product]
33 end
34 end
35
36 describe "handling GET /products.xml" do
37
38 before(:each) do
39 @product = mock_model(Product, :to_xml => "XML")
40 Product.stub!(:find).and_return(@product)
41 end
42
43 def do_get
44 @request.env["HTTP_ACCEPT"] = "application/xml"
45 get :index
46 end
47
48 it "should be successful" do
49 do_get
50 response.should be_success
51 end
52
53 it "should find all products" do
54 Product.should_receive(:find).with(:all).and_return([@product])
55 do_get
56 end
57
58 it "should render the found products as xml" do
59 @product.should_receive(:to_xml).and_return("XML")
60 do_get
61 response.body.should == "XML"
62 end
63 end
64
65 describe "handling GET /products/1" do
66
67 before(:each) do
68 @product = mock_model(Product)
69 Product.stub!(:find).and_return(@product)
70 end
71
72 def do_get
73 get :show, :id => "1"
74 end
75
76 it "should be successful" do
77 do_get
78 response.should be_success
79 end
80
81 it "should render show template" do
82 do_get
83 response.should render_template('show')
84 end
85
86 it "should find the product requested" do
87 Product.should_receive(:find).with("1").and_return(@product)
88 do_get
89 end
90
91 it "should assign the found product for the view" do
92 do_get
93 assigns[:product].should equal(@product)
94 end
95 end
96
97 describe "handling GET /products/1.xml" do
98
99 before(:each) do
100 @product = mock_model(Product, :to_xml => "XML")
101 Product.stub!(:find).and_return(@product)
102 end
103
104 def do_get
105 @request.env["HTTP_ACCEPT"] = "application/xml"
106 get :show, :id => "1"
107 end
108
109 it "should be successful" do
110 do_get
111 response.should be_success
112 end
113
114 it "should find the product requested" do
115 Product.should_receive(:find).with("1").and_return(@product)
116 do_get
117 end
118
119 it "should render the found product as xml" do
120 @product.should_receive(:to_xml).and_return("XML")
121 do_get
122 response.body.should == "XML"
123 end
124 end
125
126 describe "handling GET /products/new" do
127
128 before(:each) do
129 @product = mock_model(Product)
130 Product.stub!(:new).and_return(@product)
131 end
132
133 def do_get
134 get :new
135 end
136
137 it "should be successful" do
138 do_get
139 response.should be_success
140 end
141
142 it "should render new template" do
143 do_get
144 response.should render_template('new')
145 end
146
147 it "should create an new product" do
148 Product.should_receive(:new).and_return(@product)
149 do_get
150 end
151
152 it "should not save the new product" do
153 @product.should_not_receive(:save)
154 do_get
155 end
156
157 it "should assign the new product for the view" do
158 do_get
159 assigns[:product].should equal(@product)
160 end
161 end
162
163 describe "handling GET /products/1/edit" do
164
165 before(:each) do
166 @product = mock_model(Product)
167 Product.stub!(:find).and_return(@product)
168 end
169
170 def do_get
171 get :edit, :id => "1"
172 end
173
174 it "should be successful" do
175 do_get
176 response.should be_success
177 end
178
179 it "should render edit template" do
180 do_get
181 response.should render_template('edit')
182 end
183
184 it "should find the product requested" do
185 Product.should_receive(:find).and_return(@product)
186 do_get
187 end
188
189 it "should assign the found Product for the view" do
190 do_get
191 assigns[:product].should equal(@product)
192 end
193 end
194
195 describe "handling POST /products" do
196
197 before(:each) do
198 @product = mock_model(Product, :to_param => "1")
199 Product.stub!(:new).and_return(@product)
200 end
201
202 describe "with successful save" do
203
204 def do_post
205 @product.should_receive(:save).and_return(true)
206 post :create, :product => {}
207 end
208
209 it "should create a new product" do
210 Product.should_receive(:new).with({}).and_return(@product)
211 do_post
212 end
213
214 it "should redirect to the new product" do
215 do_post
216 response.should redirect_to(product_url("1"))
217 end
218
219 end
220
221 describe "with failed save" do
222
223 def do_post
224 @product.should_receive(:save).and_return(false)
225 post :create, :product => {}
226 end
227
228 it "should re-render 'new'" do
229 do_post
230 response.should render_template('new')
231 end
232
233 end
234 end
235
236 describe "handling PUT /products/1" do
237
238 before(:each) do
239 @product = mock_model(Product, :to_param => "1")
240 Product.stub!(:find).and_return(@product)
241 end
242
243 describe "with successful update" do
244
245 def do_put
246 @product.should_receive(:update_attributes).and_return(true)
247 put :update, :id => "1"
248 end
249
250 it "should find the product requested" do
251 Product.should_receive(:find).with("1").and_return(@product)
252 do_put
253 end
254
255 it "should update the found product" do
256 do_put
257 assigns(:product).should equal(@product)
258 end
259
260 it "should assign the found product for the view" do
261 do_put
262 assigns(:product).should equal(@product)
263 end
264
265 it "should redirect to the product" do
266 do_put
267 response.should redirect_to(product_url("1"))
268 end
269
270 end
271
272 describe "with failed update" do
273
274 def do_put
275 @product.should_receive(:update_attributes).and_return(false)
276 put :update, :id => "1"
277 end
278
279 it "should re-render 'edit'" do
280 do_put
281 response.should render_template('edit')
282 end
283
284 end
285 end
286
287 describe "handling DELETE /products/1" do
288
289 before(:each) do
290 @product = mock_model(Product, :destroy => true)
291 Product.stub!(:find).and_return(@product)
292 end
293
294 def do_delete
295 delete :destroy, :id => "1"
296 end
297
298 it "should find the product requested" do
299 Product.should_receive(:find).with("1").and_return(@product)
300 do_delete
301 end
302
303 it "should call destroy on the found product" do
304 @product.should_receive(:destroy)
305 do_delete
306 end
307
308 it "should redirect to the products list" do
309 do_delete
310 response.should redirect_to(products_url)
311 end
312 end
313end