Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 53 lines (48 sloc) 1.515 kb
bbcebcd @dchelimsky Use Rails config to determine whether or not we're using AR
dchelimsky authored
1 module RSpec
2 module Rails
a5949a5 @dchelimsky just ask if AR is defined instead
dchelimsky authored
3 if defined?(ActiveRecord)
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
4 module Extensions
5 module ActiveRecord
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
6 # Extension to enhance `should have` on AR Model classes
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
7 #
c1f0eab @dchelimsky more rdoc cleanup
dchelimsky authored
8 # @example
e26fb34 @dchelimsky small rdoc tweak
dchelimsky authored
9 #
78c7548 @dchelimsky rdoc: correct indentation helps (markdown)
dchelimsky authored
10 # ModelClass.should have(:no).records
11 # ModelClass.should have(1).record
12 # ModelClass.should have(n).records
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
13 def records
14 find(:all)
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
15 end
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
16 alias :record :records
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
17 end
96a29c4 @dchelimsky cleanup whitespace
dchelimsky authored
18
6ab0bb6 @dchelimsky cleanup and add some rdoc
dchelimsky authored
19 class ::ActiveRecord::Base
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
20 extend RSpec::Rails::Extensions::ActiveRecord
21 end
bbcebcd @dchelimsky Use Rails config to determine whether or not we're using AR
dchelimsky authored
22 end
23 end
afea2f5 @dchelimsky Add error_on and errors_on (pulled from rspec-1 and tweaked for rails-3)
dchelimsky authored
24 end
25 end
26
d41bd9b @jfirebaugh Don't assume ActiveModel is available
jfirebaugh authored
27 if defined?(::ActiveModel)
28 module ::ActiveModel::Validations
29 # Extension to enhance `should have` on AR Model instances. Calls
30 # model.valid? in order to prepare the object's errors object. Accepts
31 # a :context option to specify the validation context.
32 #
33 # You can also use this to specify the content of the error messages.
34 #
35 # @example
36 #
37 # model.should have(:no).errors_on(:attribute)
38 # model.should have(1).error_on(:attribute)
39 # model.should have(n).errors_on(:attribute)
40 # model.should have(n).errors_on(:attribute, :context => :create)
41 #
42 # model.errors_on(:attribute).should include("can't be blank")
43 def errors_on(attribute, options = {})
91e9d1e @ywen `subject.should be_valid` works correctly with ActiveResource models
ywen authored
44 valid_args = [options[:context]].compact
45 self.valid?(*valid_args)
46
d41bd9b @jfirebaugh Don't assume ActiveModel is available
jfirebaugh authored
47 [self.errors[attribute]].flatten.compact
48 end
49
50 alias :error_on :errors_on
73991af @dchelimsky Move error[s]_on to ActiveModel::Validations so it can be used by other
dchelimsky authored
51 end
52 end
Something went wrong with that request. Please try again.