public
Fork of pelargir/rspec_validation_expectations
Description: Adds several handy expectations for testing ActiveRecord model validations.
Clone URL: git://github.com/edspencer/rspec_validation_expectations.git
Added association expectations
edspencer (author)
Mon Aug 25 08:10:55 -0700 2008
commit  d8024de6b165c03078f3be9fe62ba8d74fc31501
tree    00e0310f20c67c4bd5109e3c6dbf41f4b53c4cac
parent  7e6d8ae18db85bd3609c2d65375df295a15bc558
0
...
2
3
4
 
 
 
 
 
 
5
6
7
...
20
21
22
23
 
24
25
26
 
27
28
 
 
 
 
 
 
 
 
 
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
31
32
...
2
3
4
5
6
7
8
9
10
11
12
13
...
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
0
@@ -2,6 +2,12 @@
0
 
0
 Adds several handy expectations for testing ActiveRecord model validations.
0
 
0
+== Dependencies
0
+
0
+Depends on validation_reflection plugin:
0
+
0
+ script/plugin install git://github.com/redinger/validation_reflection.git
0
+
0
 == Installation
0
 
0
 Install the gem directly:
0
@@ -20,13 +26,41 @@ Or clone the project:
0
 
0
 == Usage
0
 
0
-Ensure that validations are getting called in your models:
0
+Ensure that validations and assocations are properly defined in your models:
0
 
0
   describe User do
0
- it_should_validate_presence_of :first_name, :last_name, :email
0
+ it_should_validate_presence_of :first_name, :last_name, :email
0
     it_should_validate_numericality_of :zip
0
- it_should_validate_uniqueness_of :email
0
+ it_should_validate_uniqueness_of :email
0
+ it_should_validate_inclusion_of :gender, :in => %w(Male Female)
0
+
0
+ # tests that User.count increases by 1
0
+ it_should_be_createable :with => {:first_name => 'Ed', :last_name => 'The Duck', :email => 'a@b.com'}
0
+
0
+ it_should_belong_to :employer
0
+ it_should_have_many :friends, :romans, :countrymen
0
+ it_should_have_one :account
0
+ it_should_have_and_belong_to_many :comments
0
   end
0
+
0
+Which gives you:
0
+
0
+ruby script/spec --format s spec/models/user_spec.rb:
0
+
0
+User
0
+- should validate presence of first name
0
+- should validate presence of last name
0
+- should validate presence of email
0
+- should validate numericality of zip
0
+- should validate uniqueness of email
0
+- should validate inclusion of gender as one of Male or Female
0
+- should be creatable
0
+- should belong to employer
0
+- should have many friends
0
+- should have many romans
0
+- should have many countrymen
0
+- should have one account
0
+- should have and belong to many comments
0
 
0
 == Resources
0
 
...
1
2
 
 
 
 
3
...
 
1
2
3
4
5
6
0
@@ -1 +1,4 @@
0
-require 'rspec_validation_expectations'
0
\ No newline at end of file
0
+require 'spec/rails/matchers/validations'
0
+
0
+require 'rspec_validation_expectations'
0
+require 'rspec_association_expectations'
0
\ No newline at end of file
...
33
34
35
36
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -33,4 +33,27 @@ def it_should_validate_uniqueness_of(*one_or_more_fields)
0
       validations.collect(&:options).should include(options)
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
0
+
0
+def it_should_validate_inclusion_of(field_name, *args)
0
+ model_name = described_type
0
+ options = args.last.is_a?(Hash) ? args.pop : {}
0
+
0
+ it "should validate inclusion of #{field_name} as one of #{options[:in].to_sentence(:connector => 'or', :skip_last_comma => true)}" do
0
+ validations = model_name.reflect_on_all_validations
0
+ validation = validations.detect {|v| v.macro == :validates_inclusion_of && v.name == field_name}
0
+
0
+ unless validation.nil?
0
+ validation.options[:in].sort.should == options[:in].sort
0
+ end
0
+ end
0
+end
0
+
0
+def it_should_be_createable *args
0
+ model_name = described_type
0
+ attributes = args.last.is_a?(Hash) ? args.last[:with] : {}
0
+
0
+ it "should be creatable" do
0
+ lambda {model_name.create(attributes)}.should change(model_name, :count).by(1)
0
+ end
0
+end

Comments

    No one has commented yet.