/
spec_helper.rb
323 lines (266 loc) · 8.5 KB
/
spec_helper.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
$: << File.dirname(__FILE__) + '/../lib'
require 'rubygems'
require 'rack'
require "rails/all"
require 'rack/test'
require 'rails/test_help'
require 'rspec/rails'
RSpec::Runner.configure do |config|
config.mock_with :mocha
end
module MetaClass
def metaclass
class << self; self; end
end
end
def should_be_called(&block)
pstub = stub
pstub.expects(:call).instance_eval(&(block || proc {}))
proc { |*args| pstub.call(*args) }
end
def stub_model(name)
model = Class.new do
include Resourceful::Serialize::Model
def self.to_s
@name
end
def initialize(attrs = {})
attrs.each do |k, v|
self.stubs(k).returns(v)
end
end
def inspect
"#<#{self.class.send(:instance_variable_get, '@name')}>"
end
end
model.send(:instance_variable_set, '@name', name)
model
end
def stub_const(name)
unless Object.const_defined?(name)
obj = Object.new
obj.extend MetaClass
obj.metaclass.send(:define_method, :to_s) { name.to_s }
obj.metaclass.send(:alias_method, :inspect, :to_s)
Object.const_set(name, obj)
end
Object.const_get(name)
end
def stub_list(size, name = nil, &block)
list = Array.new(size) { |i| name ? stub("#{name}_#{i}") : stub }
list.each(&block) if block
list
end
module Spec::Matchers
def have_any(&proc)
satisfy { |a| a.any?(&proc) }
end
end
module ControllerMocks
def mock_kontroller(*to_extend)
options = to_extend.last.is_a?(Hash) ? to_extend.slice!(-1) : {}
@kontroller = Class.new
@kontroller.extend Resourceful::Maker
to_extend.each(&@kontroller.method(:extend))
@hidden_actions = Resourceful::ACTIONS.dup
@kontroller.stubs(:hidden_actions).returns(@hidden_actions)
@kontroller.stubs(:plural_action?).returns(false)
@kontroller.stubs(:include)
@kontroller.stubs(:before_filter)
@kontroller.stubs(:helper_method)
end
def mock_controller(*to_extend)
mock_kontroller
@controller = @kontroller.new
to_extend.each(&@controller.method(:extend))
end
def mock_builder(inherited = false)
@builder = stub
@builder.stubs(:response_for)
@builder.stubs(:apply)
@builder.stubs(:instance_eval).yields(@buildercc )
@builder.stubs(:inherited?).returns(inherited)
Resourceful::Base.stubs(:made_resourceful).returns([])
Resourceful::Builder.stubs(:new).returns(@builder)
end
def create_builder
@builder = Resourceful::Builder.new(@kontroller)
class << @builder
alias_method :made_resourceful, :instance_eval
end
end
def responses
@kontroller.resourceful_responses
end
def callbacks
@kontroller.resourceful_callbacks
end
def parents
@kontroller.parents
end
# Evaluates the made_resourceful block of mod (a module)
# in the context of @builder.
# @builder should be initialized via create_builder.
def made_resourceful(mod)
mod.included(@builder)
end
end
module RailsMocks
attr_reader :response, :request, :controller, :kontroller
def included(mod)
require 'ruby-debug'
debugger
end
def mock_resourceful(options = {}, &block)
options = {
:name => "things"
}.merge options
init_kontroller options
init_routes options
stub_const(options[:name].singularize.camelize)
kontroller.make_resourceful(&block)
init_controller options
end
def assigns(name)
controller.instance_variable_get("@#{name}")
end
def redirect_to(opts)
Spec::Rails::Matchers::RedirectTo.new(request, opts)
end
def render_template(path)
Spec::Rails::Matchers::RenderTemplate.new(path.to_s, @controller)
end
private
def init_kontroller(options)
@kontroller = Class.new ActionController::Base
@kontroller.extend Resourceful::Maker
@kontroller.extend MetaClass
@kontroller.metaclass.send(:define_method, :controller_name) { options[:name] }
@kontroller.metaclass.send(:define_method, :controller_path) { options[:name] }
@kontroller.metaclass.send(:define_method, :inspect) { "#{options[:name].camelize}Controller" }
@kontroller.metaclass.send(:alias_method, :to_s, :inspect)
@kontroller.send(:define_method, :controller_name) { options[:name] }
@kontroller.send(:define_method, :controller_path) { options[:name] }
@kontroller.send(:define_method, :inspect) { "#<#{options[:name].camelize}Controller>" }
@kontroller.send(:alias_method, :to_s, :inspect)
@kontroller.send(:include, ControllerMethods)
@kontroller.send(:view_paths=, [File.join(File.dirname(__FILE__), 'views')])
@kontroller
end
def init_routes(options)
ActionController::Routing::Routes.clear!
route_block = options[:routes] || proc { |map| map.resources options[:name] }
ActionController::Routing::Routes.draw(&route_block)
end
def init_controller(options)
@controller = kontroller.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller.request = @request
@controller.response = @response
@request.accept = '*/*'
@request.env['HTTP_REFERER'] = 'http://test.host'
@controller
end
def action_params(action, params = {})
params.merge case action
when :show, :edit, :destroy, {:id => 12}
when :update, {:id => 12, :thing => {}}
when :create, {:thing => {}}
else {}
end
end
def action_method(action)
method case action
when :index, :show, :edit, :new, :get
when :update, :put
when :create, :post
when :destroy, :delete
end
end
module ControllerMethods
# From rspec-rails ControllerExampleGroup
def render(options=nil, deprecated_status_or_extra_options=nil, &block)
if ::Rails::VERSION::STRING >= '2.0.0' && deprecated_status_or_extra_options.nil?
deprecated_status_or_extra_options = {}
end
unless block_given?
if @template.respond_to?(:finder)
(class << @template.finder; self; end).class_eval do
define_method :file_exists? do; true; end
end
else
(class << @template; self; end).class_eval do
define_method :file_exists? do; true; end
end
end
(class << @template; self; end).class_eval do
define_method :render_file do |*args|
@first_render ||= args[0] unless args[0] =~ /^layouts/
@_first_render ||= args[0] unless args[0] =~ /^layouts/
end
define_method :_pick_template do |*args|
@_first_render ||= args[0] unless args[0] =~ /^layouts/
PickedTemplate.new
end
end
end
super(options, deprecated_status_or_extra_options, &block)
end
class PickedTemplate
def render_template(*ignore_args); end
def render_partial(*ignore_args); end
end
end
end
module Spec::Example::ExampleGroupMethods
def should_render_html(action)
it "should render HTML by default for #{action_string(action)}" do
action_method(action)[action, action_params(action)]
response.body.should include("as HTML")
response.content_type.should == 'text/html'
end
end
def should_render_js(action)
it "should render JS for #{action_string(action)}" do
action_method(action)[action, action_params(action, :format => 'js')]
response.body.should include("insert(\"#{action}")
response.should be_success
response.content_type.should == 'text/javascript'
end
end
def shouldnt_render_xml(action)
it "shouldn't render XML for #{action_string(action)}" do
action_method(action)[action, action_params(action, :format => 'xml')]
response.should_not be_success
response.code.should == '406'
end
end
def action_string(action)
case action
when :index, "GET /things"
when :show, "GET /things/12"
when :edit, "GET /things/12/edit"
when :update, "PUT /things/12"
when :create, "POST /things"
when :new, "GET /things/new"
when :destroy, "DELETE /things/12"
end
end
end
module Spec::Example
class IntegrationExampleGroup < Spec::Example::ExampleGroup
include ActionController::TestProcess
include ActionController::Assertions
include RailsMocks
# Need this helper, because we made current_objects private
def current_objects
controller.instance_eval("current_objects")
end
# Need this helper, because we made current_object private
def current_object
controller.instance_eval("current_object")
end
ExampleGroupFactory.register(:integration, self)
end
end