public
Description: Hampton Catlins and Nex3's make_resourceful plugin
Homepage: http://groups.google.com/group/make_resourceful
Clone URL: git://github.com/jcfischer/make_resourceful.git
More integration specs.

git-svn-id: http://svn.hamptoncatlin.com/make_resourceful/trunk@158 
c18eca5a-f828-0410-9317-b2e082e89db6
nex3 (author)
Mon Nov 12 19:14:35 -0800 2007
commit  8c6d4e00f64342f647d5df9dc326171c24f30ece
tree    468403f8d310b2040686cabb835efe933ccf7121
parent  7f1c8f3b73f92a188de593151b959c3b36134c56
...
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
...
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
0
@@ -7,43 +7,163 @@ describe "ThingsController", "with all the resourceful actions" do
0
     mock_resourceful do
0
       actions :all
0
     end
0
- @object = stub('Thing')
0
- @objects = stub_list(5, 'Thing')
0
+ @objects = stub_list(5, 'Thing') do |t|
0
+ [:destroy, :save, :update_attributes].each { |m| t.stubs(m).returns(true) }
0
+ t.stubs(:to_param).returns('12')
0
+ end
0
+ @object = @objects.first
0
     Thing.stubs(:find).returns(@object)
0
+ Thing.stubs(:new).returns(@object)
0
   end
0
 
0
+ ## Default responses
0
+
0
+ (Resourceful::ACTIONS - Resourceful::MODIFYING_ACTIONS).each(&method(:should_render_html))
0
+ Resourceful::ACTIONS.each(&method(:should_render_js))
0
+ Resourceful::ACTIONS.each(&method(:shouldnt_render_xml))
0
+
0
+ ## Specs for #index
0
+
0
   it "should find all records on GET /things" do
0
     Thing.expects(:find).with(:all).returns(@objects)
0
- post :index
0
+ get :index
0
   end
0
 
0
- it "should return a list of objects for #current_object after GET /things" do
0
+ it "should return a list of objects for #current_objects after GET /things" do
0
     Thing.stubs(:find).returns(@objects)
0
- post :index
0
+ get :index
0
     controller.current_objects.should == @objects
0
   end
0
 
0
   it "should assign @things to a list of objects for GET /things" do
0
     Thing.stubs(:find).returns(@objects)
0
- post :index
0
+ get :index
0
     assigns(:things).should == @objects
0
   end
0
 
0
- it "should render HTML by default for GET /things" do
0
- post :index
0
- response.should be_success
0
- response.content_type.should == 'text/html'
0
+ ## Specs for #show
0
+
0
+ it "should find the record with id 12 on GET /things/12" do
0
+ Thing.expects(:find).with('12').returns(@object)
0
+ get :show, :id => 12
0
+ end
0
+
0
+ it "should return an object for #current_object after GET /things/12" do
0
+ Thing.stubs(:find).returns(@object)
0
+ get :show, :id => 12
0
+ controller.current_object.should == @object
0
   end
0
 
0
- it "should render JS for GET /things" do
0
- post :index, :format => 'js'
0
- response.should be_success
0
- response.content_type.should == 'text/javascript'
0
+ it "should assign @thing to an object for GET /things/12" do
0
+ Thing.stubs(:find).returns(@object)
0
+ get :show, :id => 12
0
+ assigns(:thing).should == @object
0
+ end
0
+
0
+ ## Specs for #edit
0
+
0
+ it "should find the record with id 12 on GET /things/12/edit" do
0
+ Thing.expects(:find).with('12').returns(@object)
0
+ get :edit, :id => 12
0
   end
0
 
0
- it "shouldn't render XML for GET /things" do
0
- post :index, :format => 'xml'
0
+ it "should return an object for #current_object after GET /things/12/edit" do
0
+ Thing.stubs(:find).returns(@object)
0
+ get :edit, :id => 12
0
+ controller.current_object.should == @object
0
+ end
0
+
0
+ it "should assign @thing to an object for GET /things/12/edit" do
0
+ Thing.stubs(:find).returns(@object)
0
+ get :edit, :id => 12
0
+ assigns(:thing).should == @object
0
+ end
0
+
0
+ ## Specs for #new
0
+
0
+ it "should create a new object from params[:thing] for GET /things/new" do
0
+ Thing.expects(:new).with('name' => "Herbert the thing").returns(@object)
0
+ get :new, :thing => {:name => "Herbert the thing"}
0
+ end
0
+
0
+ it "should create a new object even if there aren't any params for GET /things/new" do
0
+ Thing.expects(:new).with(nil).returns(@object)
0
+ get :new
0
+ end
0
+
0
+ it "should return the new object for #current_object after GET /things/new" do
0
+ Thing.stubs(:new).returns(@object)
0
+ get :new
0
+ controller.current_object.should == @object
0
+ end
0
+
0
+ it "should assign @thing to the new object for GET /things/new" do
0
+ Thing.stubs(:new).returns(@object)
0
+ get :new
0
+ assigns(:thing).should == @object
0
+ end
0
+
0
+ ## Specs for #create
0
+
0
+ it "should create a new object from params[:thing] for POST /things" do
0
+ Thing.expects(:new).with('name' => "Herbert the thing").returns(@object)
0
+ post :create, :thing => {:name => "Herbert the thing"}
0
+ end
0
+
0
+ it "should create a new object even if there aren't any params for POST /things" do
0
+ Thing.expects(:new).with(nil).returns(@object)
0
+ post :create
0
+ end
0
+
0
+ it "should return the new object for #current_object after POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ post :create
0
+ controller.current_object.should == @object
0
+ end
0
+
0
+ it "should assign @thing to the new object for POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ post :create
0
+ assigns(:thing).should == @object
0
+ end
0
+
0
+ it "should save the new object for POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ @object.expects(:save)
0
+ post :create
0
+ end
0
+
0
+ it "should set an appropriate flash notice for a successful POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ post :create
0
+ flash[:notice].should == "Create successful!"
0
+ end
0
+
0
+ it "should redirect to the new object for a successful POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ post :create
0
+ response.should redirect_to('/things/12')
0
+ end
0
+
0
+ it "should set an appropriate flash error for an unsuccessful POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ @object.stubs(:save).returns(false)
0
+ post :create
0
+ flash[:error].should == "There was a problem!"
0
+ end
0
+
0
+ it "should give a failing response for an unsuccessful POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ @object.stubs(:save).returns(false)
0
+ post :create
0
     response.should_not be_success
0
- response.code.should == '406'
0
+ response.code.should == '422'
0
+ end
0
+
0
+ it "should render the #new template for an unsuccessful POST /things" do
0
+ Thing.stubs(:new).returns(@object)
0
+ @object.stubs(:save).returns(false)
0
+ post :create
0
+ response.should render_template('new')
0
   end
0
 end
...
1
2
3
 
 
4
5
6
...
35
36
37
38
 
 
 
 
 
 
39
40
41
42
43
 
 
 
 
44
45
46
...
109
110
111
 
 
 
 
 
112
113
114
...
127
128
129
 
 
 
 
 
 
 
 
130
131
132
...
161
162
163
 
164
165
166
167
 
 
 
 
 
 
 
 
 
168
169
170
...
180
181
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
 
3
4
5
6
7
...
36
37
38
 
39
40
41
42
43
44
45
46
47
 
 
48
49
50
51
52
53
54
...
117
118
119
120
121
122
123
124
125
126
127
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
...
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
0
@@ -1,6 +1,7 @@
0
 $: << File.dirname(__FILE__) + '/../lib'
0
 %w[rubygems spec action_pack active_record resourceful/maker
0
- action_controller action_controller/test_process action_controller/integration].each &method(:require)
0
+ action_controller action_controller/test_process action_controller/integration
0
+ spec/rspec_on_rails/redirect_to spec/rspec_on_rails/render_template].each &method(:require)
0
 
0
 Spec::Runner.configure do |config|
0
   config.mock_with :mocha
0
@@ -35,12 +36,19 @@ def stub_model(name)
0
 end
0
 
0
 def stub_const(name)
0
- Object.const_set(name, stub(name.to_s)) unless Object.const_defined?(name)
0
+ unless Object.const_defined?(name)
0
+ obj = Object.new
0
+ obj.metaclass.send(:define_method, :to_s) { name.to_s }
0
+ obj.metaclass.send(:alias_method, :inspect, :to_s)
0
+ Object.const_set(name, obj)
0
+ end
0
   Object.const_get(name)
0
 end
0
 
0
-def stub_list(size, name = nil)
0
- Array.new(size) { |i| name ? stub("#{name}_#{i}") : stub }
0
+def stub_list(size, name = nil, &block)
0
+ list = Array.new(size) { |i| name ? stub("#{name}_#{i}") : stub }
0
+ list.each(&block) if block
0
+ list
0
 end
0
 
0
 module Spec::Matchers
0
@@ -109,6 +117,11 @@ end
0
 module RailsMocks
0
   attr_reader :response, :request, :controller, :kontroller
0
 
0
+ def included(mod)
0
+ require 'ruby-debug'
0
+ debugger
0
+ end
0
+
0
   def mock_resourceful(options = {}, &block)
0
     options = {
0
       :name => "things"
0
@@ -127,6 +140,14 @@ module RailsMocks
0
     controller.instance_variable_get("@#{name}")
0
   end
0
 
0
+ def redirect_to(opts)
0
+ RedirectTo.new(request, opts)
0
+ end
0
+
0
+ def render_template(path)
0
+ RenderTemplate.new(path.to_s, @controller)
0
+ end
0
+
0
   private
0
 
0
   def init_kontroller(options)
0
@@ -161,10 +182,20 @@ module RailsMocks
0
     @controller.request = @request
0
     @controller.response = @response
0
     @request.accept = '*/*'
0
+ @request.env['HTTP_REFERER'] = 'http://test.host'
0
 
0
     @controller
0
   end
0
 
0
+ def action_params(action, params = {})
0
+ params.merge case action
0
+ when :show, :edit, :destroy: {:id => 12}
0
+ when :update: {:id => 12, :thing => {}}
0
+ when :create: {:thing => {}}
0
+ else {}
0
+ end
0
+ end
0
+
0
   module ControllerMethods
0
     def render(options=nil, deprecated_status=nil, &block)
0
       unless block_given?
0
@@ -180,3 +211,41 @@ module RailsMocks
0
     end
0
   end
0
 end
0
+
0
+module Spec::DSL::BehaviourEval::ModuleMethods
0
+ def should_render_html(action)
0
+ it "should render HTML by default for #{action_string(action)}" do
0
+ get action, action_params(action)
0
+ response.should be_success
0
+ response.content_type.should == 'text/html'
0
+ end
0
+ end
0
+
0
+ def should_render_js(action)
0
+ it "should render JS for #{action_string(action)}" do
0
+ get action, action_params(action, :format => 'js')
0
+ response.should be_success
0
+ response.content_type.should == 'text/javascript'
0
+ end
0
+ end
0
+
0
+ def shouldnt_render_xml(action)
0
+ it "should render XML for #{action_string(action)}" do
0
+ get action, action_params(action, :format => 'xml')
0
+ response.should_not be_success
0
+ response.code.should == '406'
0
+ end
0
+ end
0
+
0
+ def action_string(action)
0
+ case action
0
+ when :index: "GET /things"
0
+ when :show: "GET /things/12"
0
+ when :edit: "GET /things/12/edit"
0
+ when :update: "PUT /things/12"
0
+ when :create: "POST /things"
0
+ when :new: "GET /things/new"
0
+ when :destroy: "DELETE /things/12"
0
+ end
0
+ end
0
+end

Comments

    No one has commented yet.