jcfischer / make_resourceful

Hampton Catlins and Nex3's make_resourceful plugin

This URL has Read+Write access

make_resourceful / spec / spec_helper.rb
100644 280 lines (232 sloc) 7.538 kb
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
$: << File.dirname(__FILE__) + '/../lib'
require 'rubygems'
%w[spec action_pack active_record resourceful/maker
action_controller action_controller/test_process action_controller/integration
spec/rspec_on_rails/redirect_to spec/rspec_on_rails/render_template].each &method(:require)
 
Spec::Runner.configure do |config|
  config.mock_with :mocha
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.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.read_inheritable_attribute(:resourceful_responses)
  end
 
  def callbacks
    @kontroller.read_inheritable_attribute(:resourceful_callbacks)
  end
 
  def parents
    @kontroller.read_inheritable_attribute(: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)
    RedirectTo.new(request, opts)
  end
 
  def render_template(path)
    RenderTemplate.new(path.to_s, @controller)
  end
 
  private
 
  def init_kontroller(options)
    @kontroller = Class.new ActionController::Base
    @kontroller.extend Resourceful::Maker
 
    @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
  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
    def render(options=nil, deprecated_status=nil, &block)
      unless block_given?
        @template.metaclass.class_eval do
          define_method :file_exists? do true end
          define_method :render_file do |*args|
            @first_render ||= args[0]
          end
        end
      end
 
      super(options, deprecated_status, &block)
    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.should be_success
      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.should be_success
      response.content_type.should == 'text/javascript'
    end
  end
 
  def shouldnt_render_xml(action)
    it "should 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 < Test::Unit::TestCase
    include ExampleMethods
    class << self
      include ExampleGroupMethods
    end
 
    def initialize(defined_description, &implementation)
      super()
      @_defined_description = defined_description
      @_implementation = implementation
    end
 
    ExampleGroupFactory.register(:integration, self)
  end
end