Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec_on_rails / generators / rspec_scaffold / templates / controller_spec.rb
blobb15ba8e6ec16fbc6d97cdfe0c49354211cc296fe
1 require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper'
3 describe <%= controller_class_name %>Controller, "#route_for" do
5   it "should map { :controller => '<%= table_name %>', :action => 'index' } to /<%= table_name %>" do
6     route_for(:controller => "<%= table_name %>", :action => "index").should == "/<%= table_name %>"
7   end
8   
9   it "should map { :controller => '<%= table_name %>', :action => 'new' } to /<%= table_name %>/new" do
10     route_for(:controller => "<%= table_name %>", :action => "new").should == "/<%= table_name %>/new"
11   end
12   
13   it "should map { :controller => '<%= table_name %>', :action => 'show', :id => 1 } to /<%= table_name %>/1" do
14     route_for(:controller => "<%= table_name %>", :action => "show", :id => 1).should == "/<%= table_name %>/1"
15   end
16   
17   it "should map { :controller => '<%= table_name %>', :action => 'edit', :id => 1 } to /<%= table_name %>/1<%= resource_edit_path %>" do
18     route_for(:controller => "<%= table_name %>", :action => "edit", :id => 1).should == "/<%= table_name %>/1<%= resource_edit_path %>"
19   end
20   
21   it "should map { :controller => '<%= table_name %>', :action => 'update', :id => 1} to /<%= table_name %>/1" do
22     route_for(:controller => "<%= table_name %>", :action => "update", :id => 1).should == "/<%= table_name %>/1"
23   end
24   
25   it "should map { :controller => '<%= table_name %>', :action => 'destroy', :id => 1} to /<%= table_name %>/1" do
26     route_for(:controller => "<%= table_name %>", :action => "destroy", :id => 1).should == "/<%= table_name %>/1"
27   end
28   
29 end
31 describe <%= controller_class_name %>Controller, "#params_from" do
33   it "should generate params { :controller => '<%= table_name %>', action => 'index' } from GET /<%= table_name %>" do
34     params_from(:get, "/<%= table_name %>").should == {:controller => "<%= table_name %>", :action => "index"}
35   end
36   
37   it "should generate params { :controller => '<%= table_name %>', action => 'new' } from GET /<%= table_name %>/new" do
38     params_from(:get, "/<%= table_name %>/new").should == {:controller => "<%= table_name %>", :action => "new"}
39   end
40   
41   it "should generate params { :controller => '<%= table_name %>', action => 'create' } from POST /<%= table_name %>" do
42     params_from(:post, "/<%= table_name %>").should == {:controller => "<%= table_name %>", :action => "create"}
43   end
44   
45   it "should generate params { :controller => '<%= table_name %>', action => 'show', id => '1' } from GET /<%= table_name %>/1" do
46     params_from(:get, "/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => "show", :id => "1"}
47   end
48   
49   it "should generate params { :controller => '<%= table_name %>', action => 'edit', id => '1' } from GET /<%= table_name %>/1;edit" do
50     params_from(:get, "/<%= table_name %>/1<%= resource_edit_path %>").should == {:controller => "<%= table_name %>", :action => "edit", :id => "1"}
51   end
52   
53   it "should generate params { :controller => '<%= table_name %>', action => 'update', id => '1' } from PUT /<%= table_name %>/1" do
54     params_from(:put, "/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => "update", :id => "1"}
55   end
56   
57   it "should generate params { :controller => '<%= table_name %>', action => 'destroy', id => '1' } from DELETE /<%= table_name %>/1" do
58     params_from(:delete, "/<%= table_name %>/1").should == {:controller => "<%= table_name %>", :action => "destroy", :id => "1"}
59   end
60   
61 end
63 describe <%= controller_class_name %>Controller, "handling GET /<%= table_name %>" do
65   before do
66     @<%= file_name %> = mock_model(<%= class_name %>)
67     <%= class_name %>.stub!(:find).and_return([@<%= file_name %>])
68   end
69   
70   def do_get
71     get :index
72   end
73   
74   it "should be successful" do
75     do_get
76     response.should be_success
77   end
79   it "should render index template" do
80     do_get
81     response.should render_template('index')
82   end
83   
84   it "should find all <%= table_name %>" do
85     <%= class_name %>.should_receive(:find).with(:all).and_return([@<%= file_name %>])
86     do_get
87   end
88   
89   it "should assign the found <%= table_name %> for the view" do
90     do_get
91     assigns[:<%= table_name %>].should == [@<%= file_name %>]
92   end
93 end
95 describe <%= controller_class_name %>Controller, "handling GET /<%= table_name %>.xml" do
97   before do
98     @<%= file_name %> = mock_model(<%= class_name %>, :to_xml => "XML")
99     <%= class_name %>.stub!(:find).and_return(@<%= file_name %>)
100   end
101   
102   def do_get
103     @request.env["HTTP_ACCEPT"] = "application/xml"
104     get :index
105   end
106   
107   it "should be successful" do
108     do_get
109     response.should be_success
110   end
112   it "should find all <%= table_name %>" do
113     <%= class_name %>.should_receive(:find).with(:all).and_return([@<%= file_name %>])
114     do_get
115   end
116   
117   it "should render the found <%= table_name %> as xml" do
118     @<%= file_name %>.should_receive(:to_xml).and_return("XML")
119     do_get
120     response.body.should == "XML"
121   end
124 describe <%= controller_class_name %>Controller, "handling GET /<%= table_name %>/1" do
126   before do
127     @<%= file_name %> = mock_model(<%= class_name %>)
128     <%= class_name %>.stub!(:find).and_return(@<%= file_name %>)
129   end
130   
131   def do_get
132     get :show, :id => "1"
133   end
135   it "should be successful" do
136     do_get
137     response.should be_success
138   end
139   
140   it "should render show template" do
141     do_get
142     response.should render_template('show')
143   end
144   
145   it "should find the <%= file_name %> requested" do
146     <%= class_name %>.should_receive(:find).with("1").and_return(@<%= file_name %>)
147     do_get
148   end
149   
150   it "should assign the found <%= file_name %> for the view" do
151     do_get
152     assigns[:<%= file_name %>].should equal(@<%= file_name %>)
153   end
156 describe <%= controller_class_name %>Controller, "handling GET /<%= table_name %>/1.xml" do
158   before do
159     @<%= file_name %> = mock_model(<%= class_name %>, :to_xml => "XML")
160     <%= class_name %>.stub!(:find).and_return(@<%= file_name %>)
161   end
162   
163   def do_get
164     @request.env["HTTP_ACCEPT"] = "application/xml"
165     get :show, :id => "1"
166   end
168   it "should be successful" do
169     do_get
170     response.should be_success
171   end
172   
173   it "should find the <%= file_name %> requested" do
174     <%= class_name %>.should_receive(:find).with("1").and_return(@<%= file_name %>)
175     do_get
176   end
177   
178   it "should render the found <%= file_name %> as xml" do
179     @<%= file_name %>.should_receive(:to_xml).and_return("XML")
180     do_get
181     response.body.should == "XML"
182   end
185 describe <%= controller_class_name %>Controller, "handling GET /<%= table_name %>/new" do
187   before do
188     @<%= file_name %> = mock_model(<%= class_name %>)
189     <%= class_name %>.stub!(:new).and_return(@<%= file_name %>)
190   end
191   
192   def do_get
193     get :new
194   end
196   it "should be successful" do
197     do_get
198     response.should be_success
199   end
200   
201   it "should render new template" do
202     do_get
203     response.should render_template('new')
204   end
205   
206   it "should create an new <%= file_name %>" do
207     <%= class_name %>.should_receive(:new).and_return(@<%= file_name %>)
208     do_get
209   end
210   
211   it "should not save the new <%= file_name %>" do
212     @<%= file_name %>.should_not_receive(:save)
213     do_get
214   end
215   
216   it "should assign the new <%= file_name %> for the view" do
217     do_get
218     assigns[:<%= file_name %>].should equal(@<%= file_name %>)
219   end
222 describe <%= controller_class_name %>Controller, "handling GET /<%= table_name %>/1/edit" do
224   before do
225     @<%= file_name %> = mock_model(<%= class_name %>)
226     <%= class_name %>.stub!(:find).and_return(@<%= file_name %>)
227   end
228   
229   def do_get
230     get :edit, :id => "1"
231   end
233   it "should be successful" do
234     do_get
235     response.should be_success
236   end
237   
238   it "should render edit template" do
239     do_get
240     response.should render_template('edit')
241   end
242   
243   it "should find the <%= file_name %> requested" do
244     <%= class_name %>.should_receive(:find).and_return(@<%= file_name %>)
245     do_get
246   end
247   
248   it "should assign the found <%= class_name %> for the view" do
249     do_get
250     assigns[:<%= file_name %>].should equal(@<%= file_name %>)
251   end
254 describe <%= controller_class_name %>Controller, "handling POST /<%= table_name %>" do
256   before do
257     @<%= file_name %> = mock_model(<%= class_name %>, :to_param => "1")
258     <%= class_name %>.stub!(:new).and_return(@<%= file_name %>)
259   end
260   
261   def post_with_successful_save
262     @<%= file_name %>.should_receive(:save).and_return(true)
263     post :create, :<%= file_name %> => {}
264   end
265   
266   def post_with_failed_save
267     @<%= file_name %>.should_receive(:save).and_return(false)
268     post :create, :<%= file_name %> => {}
269   end
270   
271   it "should create a new <%= file_name %>" do
272     <%= class_name %>.should_receive(:new).with({}).and_return(@<%= file_name %>)
273     post_with_successful_save
274   end
276   it "should redirect to the new <%= file_name %> on successful save" do
277     post_with_successful_save
278     response.should redirect_to(<%= table_name.singularize %>_url("1"))
279   end
281   it "should re-render 'new' on failed save" do
282     post_with_failed_save
283     response.should render_template('new')
284   end
287 describe <%= controller_class_name %>Controller, "handling PUT /<%= table_name %>/1" do
289   before do
290     @<%= file_name %> = mock_model(<%= class_name %>, :to_param => "1")
291     <%= class_name %>.stub!(:find).and_return(@<%= file_name %>)
292   end
293   
294   def put_with_successful_update
295     @<%= file_name %>.should_receive(:update_attributes).and_return(true)
296     put :update, :id => "1"
297   end
298   
299   def put_with_failed_update
300     @<%= file_name %>.should_receive(:update_attributes).and_return(false)
301     put :update, :id => "1"
302   end
303   
304   it "should find the <%= file_name %> requested" do
305     <%= class_name %>.should_receive(:find).with("1").and_return(@<%= file_name %>)
306     put_with_successful_update
307   end
309   it "should update the found <%= file_name %>" do
310     put_with_successful_update
311     assigns(:<%= file_name %>).should equal(@<%= file_name %>)
312   end
314   it "should assign the found <%= file_name %> for the view" do
315     put_with_successful_update
316     assigns(:<%= file_name %>).should equal(@<%= file_name %>)
317   end
319   it "should redirect to the <%= file_name %> on successful update" do
320     put_with_successful_update
321     response.should redirect_to(<%= table_name.singularize %>_url("1"))
322   end
324   it "should re-render 'edit' on failed update" do
325     put_with_failed_update
326     response.should render_template('edit')
327   end
330 describe <%= controller_class_name %>Controller, "handling DELETE /<%= table_name %>/1" do
332   before do
333     @<%= file_name %> = mock_model(<%= class_name %>, :destroy => true)
334     <%= class_name %>.stub!(:find).and_return(@<%= file_name %>)
335   end
336   
337   def do_delete
338     delete :destroy, :id => "1"
339   end
341   it "should find the <%= file_name %> requested" do
342     <%= class_name %>.should_receive(:find).with("1").and_return(@<%= file_name %>)
343     do_delete
344   end
345   
346   it "should call destroy on the found <%= file_name %>" do
347     @<%= file_name %>.should_receive(:destroy)
348     do_delete
349   end
350   
351   it "should redirect to the <%= table_name %> list" do
352     do_delete
353     response.should redirect_to(<%= table_name %>_url)
354   end