public
Description: Custom controller spec macros, with an included 'custom_scaffold' generator.
Clone URL: git://github.com/technoweenie/rspec_on_rails_on_crack.git
add beginnings of model validation helpers
technoweenie (author)
Wed Dec 26 12:25:32 -0800 2007
commit  7ca5003d2bacd8671c22f56e40f5ac8161d723e9
tree    773fc7d7e4807d1e9fa35b5e8ce2997664e0d01e
parent  59e7f7450fb6989b236ab93c05e5406ed14a14ea
...
1
 
2
3
4
...
 
1
2
3
4
0
@@ -1,4 +1,4 @@
0
-%w(behaviors example_group_methods example).each { |lib| require "rspec_on_rails_on_crack/#{lib}" }
0
+%w(behaviors example_group_methods example main).each { |lib| require "rspec_on_rails_on_crack/#{lib}" }
0
 
0
 Spec::Runner.configuration.include RspecOnRailsOnCrack::Behaviors
0
 
...
115
116
117
 
 
 
 
 
 
 
 
 
 
 
118
119
120
...
217
218
219
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
221
222
223
224
 
225
226
227
228
229
230
231
 
232
233
234
235
 
236
237
238
...
277
278
279
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
282
283
...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
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
...
320
321
322
 
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
0
@@ -115,6 +115,17 @@ class Spec::Example::AwesomeExample < Spec::Example::Example
0
     raise NotImplementedError
0
   end
0
 
0
+ #
0
+ # it.validates_presence_of Foo, :name
0
+ #
0
+ def validates_presence_of(model, *attributes)
0
+ attributes.each do |attr|
0
+ desc, imp = assert_validates_presence_of(model, attr)
0
+ if @defined_description then create_sub_example desc, &imp
0
+ else @defined_description, @implementation = desc, imp end
0
+ end
0
+ end
0
+
0
   @@variable_types.each do |collection_type, collection_op|
0
     public
0
     define_method "assigns_#{collection_type}" do |values|
0
@@ -217,22 +228,54 @@ protected
0
   # it attempts to match the mime type using Mime::Type.lookup_by_extension.
0
   #
0
   def render_xml(record = nil, options = {}, &block)
0
+ render_xml_or_json :xml, record, options, &block
0
+ end
0
+
0
+ # Creates 3 examples: One to check that the given JSON was returned.
0
+ # It looks for two options: :status and :format.
0
+ #
0
+ # Checks that the json matches a given string
0
+ #
0
+ # it.renders(:json) { "{}" }
0
+ #
0
+ # Checks that the json matches @foo.to_json
0
+ #
0
+ # it.renders :json, :foo
0
+ #
0
+ # Checks that the json matches @foo.errors.to_json
0
+ #
0
+ # it.renders :json, "foo.errors"
0
+ #
0
+ # it.renders :json, :index, :status => :not_found
0
+ # it.renders :json, :index, :format => :json
0
+ #
0
+ # If :status is unset, it checks that that the response was a success.
0
+ # Otherwise it takes an integer or a symbol and matches the status code.
0
+ #
0
+ # If :format is unset, it checks that the action is Mime:HTML. Otherwise
0
+ # it attempts to match the mime type using Mime::Type.lookup_by_extension.
0
+ #
0
+ def render_json(record = nil, options = {}, &block)
0
+ render_xml_or_json :json, record, options, &block
0
+ end
0
+
0
+ def render_xml_or_json(format, record = nil, options = {}, &block)
0
     if record.is_a?(Hash)
0
       options = record
0
       record = nil
0
     end
0
- @defined_description = "renders xml"
0
+ @defined_description = "renders #{format}"
0
     @implementation = lambda do
0
       if record
0
         pieces = record.to_s.split(".")
0
         record = instance_variable_get("@#{pieces.shift}")
0
         record = record.send(pieces.shift) until pieces.empty?
0
       end
0
- block ||= lambda { record.to_xml }
0
+ block ||= lambda { record.send("to_#{format}") }
0
       acting.should have_text(block.call)
0
     end
0
     assert_status options[:status]
0
- assert_content_type options[:format] || :xml
0
+ assert_content_type options[:format] || format
0
   end
0
 
0
   def assigns_example_values(name, value)
0
@@ -277,7 +320,21 @@ protected
0
       end
0
     end
0
   end
0
-
0
+
0
+ def assert_validates_presence_of(model, attribute)
0
+ ["requires #{attribute}", lambda do
0
+ @attributes.each do |key, value|
0
+ @record.send("#{key}=", value)
0
+ end
0
+ old_value = @record.send(attribute)
0
+ @record.send("#{attribute}=", nil)
0
+ @record.should_not be_valid
0
+ @record.errors.on(attribute).should_not be_nil
0
+ @record.send("#{attribute}=", old_value)
0
+ @record.should be_valid
0
+ end]
0
+ end
0
+
0
   def create_sub_example(desc, &imp)
0
     @example_group.send(:example_objects) << Spec::Example::AwesomeExample.new(desc, @example_group, &imp) if @example_group
0
   end

Comments

    No one has commented yet.