Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 45 lines (41 sloc) 1.355 kb
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
1 if defined?(ActiveRecord::Base)
a321c9c @dchelimsky change top-level module from Rspec to RSpec
dchelimsky authored
2 module RSpec
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
3 module Rails
4 module Extensions
5 module ActiveRecord
6 module ClassMethods
7 # :call-seq:
8 # ModelClass.should have(:no).records
9 # ModelClass.should have(1).record
10 # ModelClass.should have(n).records
11 #
12 # Extension to enhance <tt>should have</tt> on AR Model classes
13 def records
14 find(:all)
15 end
16 alias :record :records
17 end
18
19 module InstanceMethods
20 # :call-seq:
21 # model.should have(:no).errors_on(:attribute)
22 # model.should have(1).error_on(:attribute)
23 # model.should have(n).errors_on(:attribute)
24 #
25 # Extension to enhance <tt>should have</tt> on AR Model instances.
26 # Calls model.valid? in order to prepare the object's errors
27 # object.
28 def errors_on(attribute)
29 self.valid?
30 [self.errors[attribute]].flatten.compact
31 end
32 alias :error_on :errors_on
33 end
34 end
35 end
36 end
37 end
96a29c4 @dchelimsky cleanup whitespace
dchelimsky authored
38
83f2a44 @dchelimsky don't define AR extensions unless AR is defined
dchelimsky authored
39 class ActiveRecord::Base #:nodoc:
40 extend RSpec::Rails::Extensions::ActiveRecord::ClassMethods
41 include RSpec::Rails::Extensions::ActiveRecord::InstanceMethods
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
42 end
43 end
44
Something went wrong with that request. Please try again.