shoulda-matchers 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +13 -0
  3. data/Appraisals +4 -6
  4. data/CONTRIBUTING.md +38 -0
  5. data/Gemfile +8 -5
  6. data/Gemfile.lock +77 -41
  7. data/NEWS.md +32 -0
  8. data/README.md +84 -0
  9. data/Rakefile +5 -36
  10. data/features/rails_integration.feature +88 -0
  11. data/features/step_definitions/rails_steps.rb +111 -0
  12. data/features/support/env.rb +5 -0
  13. data/gemfiles/3.0.gemfile +14 -0
  14. data/gemfiles/3.0.gemfile.lock +142 -0
  15. data/gemfiles/3.1.gemfile +16 -0
  16. data/gemfiles/3.1.gemfile.lock +164 -0
  17. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +6 -9
  18. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +1 -3
  19. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +10 -6
  20. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +1 -4
  21. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
  22. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +11 -10
  23. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +0 -2
  24. data/lib/shoulda/matchers/action_controller/route_matcher.rb +17 -14
  25. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +24 -16
  26. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +52 -15
  27. data/lib/shoulda/matchers/action_mailer.rb +1 -1
  28. data/lib/shoulda/matchers/action_mailer/{have_sent_email.rb → have_sent_email_matcher.rb} +37 -21
  29. data/lib/shoulda/matchers/active_model.rb +1 -0
  30. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +9 -10
  31. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +60 -33
  32. data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +0 -1
  33. data/lib/shoulda/matchers/active_model/helpers.rb +13 -9
  34. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +63 -0
  35. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +38 -8
  36. data/lib/shoulda/matchers/active_model/validation_matcher.rb +1 -5
  37. data/lib/shoulda/matchers/active_record.rb +3 -1
  38. data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +127 -0
  39. data/lib/shoulda/matchers/active_record/association_matcher.rb +19 -7
  40. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +20 -5
  41. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +4 -10
  42. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +3 -7
  43. data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +107 -0
  44. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +111 -0
  45. data/lib/shoulda/matchers/integrations/rspec.rb +0 -1
  46. data/lib/shoulda/matchers/version.rb +1 -1
  47. data/shoulda-matchers.gemspec +30 -0
  48. data/spec/fixtures/addresses.yml +3 -0
  49. data/spec/fixtures/friendships.yml +0 -0
  50. data/spec/fixtures/posts.yml +5 -0
  51. data/spec/fixtures/products.yml +0 -0
  52. data/spec/fixtures/taggings.yml +0 -0
  53. data/spec/fixtures/tags.yml +9 -0
  54. data/spec/fixtures/users.yml +6 -0
  55. data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +61 -0
  56. data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +20 -0
  57. data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +40 -0
  58. data/spec/shoulda/action_controller/render_template_matcher_spec.rb +69 -0
  59. data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +47 -0
  60. data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +28 -0
  61. data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +83 -0
  62. data/spec/shoulda/action_controller/route_matcher_spec.rb +65 -0
  63. data/spec/shoulda/action_controller/set_session_matcher_spec.rb +46 -0
  64. data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +124 -0
  65. data/spec/shoulda/action_mailer/have_sent_email_spec.rb +293 -0
  66. data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +95 -0
  67. data/spec/shoulda/active_model/allow_value_matcher_spec.rb +91 -0
  68. data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +57 -0
  69. data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +71 -0
  70. data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +125 -0
  71. data/spec/shoulda/active_model/helpers_spec.rb +100 -0
  72. data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +43 -0
  73. data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +48 -0
  74. data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +38 -0
  75. data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +62 -0
  76. data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +121 -0
  77. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +143 -0
  78. data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +84 -0
  79. data/spec/shoulda/active_record/association_matcher_spec.rb +449 -0
  80. data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +185 -0
  81. data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +88 -0
  82. data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +46 -0
  83. data/spec/shoulda/active_record/query_the_database_matcher_spec.rb +45 -0
  84. data/spec/shoulda/active_record/serialize_matcher_spec.rb +81 -0
  85. data/spec/spec_helper.rb +31 -0
  86. data/spec/support/model_builder.rb +149 -0
  87. metadata +211 -60
  88. data/CONTRIBUTION_GUIDELINES.rdoc +0 -10
  89. data/README.rdoc +0 -80
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveModel::ValidateAcceptanceOfMatcher do
4
+
5
+ context "an attribute which must be accepted" do
6
+ before do
7
+ @model = define_model(:example, :attr => :string) do
8
+ validates_acceptance_of :attr
9
+ end.new
10
+ end
11
+
12
+ it "should require that attribute to be accepted" do
13
+ @model.should validate_acceptance_of(:attr)
14
+ end
15
+
16
+ it "should not overwrite the default message with nil" do
17
+ @model.should validate_acceptance_of(:attr).with_message(nil)
18
+ end
19
+ end
20
+
21
+ context "an attribute that does not need to be accepted" do
22
+ before do
23
+ @model = define_model(:example, :attr => :string).new
24
+ end
25
+
26
+ it "should not require that attribute to be accepted" do
27
+ @model.should_not validate_acceptance_of(:attr)
28
+ end
29
+ end
30
+
31
+ context "an attribute which must be accepted with a custom message" do
32
+ before do
33
+ @model = define_model(:example, :attr => :string) do
34
+ validates_acceptance_of :attr, :message => 'custom'
35
+ end.new
36
+ end
37
+
38
+ it "should require that attribute to be accepted with that message" do
39
+ @model.should validate_acceptance_of(:attr).with_message(/custom/)
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveModel::ValidateConfirmationOfMatcher do
4
+
5
+ context "an attribute which needs confirmation" do
6
+ before do
7
+ define_model(:example, :attr => :string) do
8
+ validates_confirmation_of :attr
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ it "should require confirmation of that attribute" do
14
+ @model.should validate_confirmation_of(:attr)
15
+ end
16
+
17
+ it "should not override the default message with a blank" do
18
+ @model.should validate_confirmation_of(:attr).with_message(nil)
19
+ end
20
+ end
21
+
22
+ context "an attribute which must be confirmed with a custom message" do
23
+ before do
24
+ define_model :example, :attr => :string do
25
+ validates_confirmation_of :attr, :message => 'custom'
26
+ end
27
+ @model = Example.new
28
+ end
29
+
30
+ it "should require confirmation of that attribute with that message" do
31
+ @model.should validate_confirmation_of(:attr).with_message(/custom/)
32
+ end
33
+
34
+ it "should not require confirmation of that attribute with another message" do
35
+ @model.should_not validate_confirmation_of(:attr)
36
+ end
37
+ end
38
+
39
+ context "an attribute which doesn't need confirmation" do
40
+ before do
41
+ @model = define_model(:example, :attr => :string).new
42
+ end
43
+
44
+ it "should not require confirmation of that attribute" do
45
+ @model.should_not validate_confirmation_of(:attr)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveModel::ValidateFormatOfMatcher do
4
+
5
+ context "a postal code" do
6
+ before do
7
+ define_model :example, :attr => :string do
8
+ validates_format_of :attr, :with => /^\d{5}$/
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ it "should be valid" do
14
+ @model.should validate_format_of(:attr).with('12345')
15
+ end
16
+
17
+ it "should not be valid with alpha in zip" do
18
+ @model.should_not validate_format_of(:attr).with('1234a')
19
+ @model.should validate_format_of(:attr).not_with('1234a')
20
+ end
21
+
22
+ it "should not be valid with too few digits" do
23
+ @model.should_not validate_format_of(:attr).with('1234')
24
+ @model.should validate_format_of(:attr).not_with('1234')
25
+ end
26
+
27
+ it "should not be valid with too many digits" do
28
+ @model.should_not validate_format_of(:attr).with('123456')
29
+ @model.should validate_format_of(:attr).not_with('123456')
30
+ end
31
+
32
+ it "should raise error if you try to call both with and not_with" do
33
+ expect { validate_format_of(:attr).not_with('123456').with('12345') }.
34
+ to raise_error(RuntimeError)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
4
+
5
+ context "a numeric attribute" do
6
+ before do
7
+ define_model :example, :attr => :string do
8
+ validates_numericality_of :attr
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ it "should only allow numeric values for that attribute" do
14
+ @model.should validate_numericality_of(:attr)
15
+ end
16
+
17
+ it "should not override the default message with a blank" do
18
+ @model.should validate_numericality_of(:attr).with_message(nil)
19
+ end
20
+ end
21
+
22
+ context "a numeric attribute which must be integer" do
23
+ before do
24
+ define_model :example, :attr => :string do
25
+ validates_numericality_of :attr, { :only_integer => true }
26
+ end
27
+ @model = Example.new
28
+ end
29
+
30
+ it "should only allow integer values for that attribute" do
31
+ @model.should validate_numericality_of(:attr).only_integer
32
+ end
33
+ end
34
+
35
+ context "a numeric attribute with a custom validation message" do
36
+ before do
37
+ define_model :example, :attr => :string do
38
+ validates_numericality_of :attr, :message => 'custom'
39
+ end
40
+ @model = Example.new
41
+ end
42
+
43
+ it "should only allow numeric values for that attribute with that message" do
44
+ @model.should validate_numericality_of(:attr).with_message(/custom/)
45
+ end
46
+
47
+ it "should not allow numeric values for that attribute with another message" do
48
+ @model.should_not validate_numericality_of(:attr)
49
+ end
50
+ end
51
+
52
+ context "a non-numeric attribute" do
53
+ before do
54
+ @model = define_model(:example, :attr => :string).new
55
+ end
56
+
57
+ it "should not only allow numeric values for that attribute" do
58
+ @model.should_not validate_numericality_of(:attr)
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
4
+
5
+ context "a required attribute" do
6
+ before do
7
+ define_model :example, :attr => :string do
8
+ validates_presence_of :attr
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ it "should require a value" do
14
+ @model.should validate_presence_of(:attr)
15
+ end
16
+
17
+ it "should not override the default message with a blank" do
18
+ @model.should validate_presence_of(:attr).with_message(nil)
19
+ end
20
+ end
21
+
22
+ context "a required attribute on a class using ActiveModel::Validations" do
23
+ before do
24
+ define_active_model_class("Example", :accessors => [:attr]) do
25
+ validates_presence_of :attr
26
+ end
27
+ @model = Example.new
28
+ end
29
+
30
+ it "should require a value" do
31
+ @model.should validate_presence_of(:attr)
32
+ end
33
+
34
+ it "should not override the default message with a blank" do
35
+ @model.should validate_presence_of(:attr).with_message(nil)
36
+ end
37
+ end
38
+
39
+ context "an optional attribute" do
40
+ before do
41
+ @model = define_model(:example, :attr => :string).new
42
+ end
43
+
44
+ it "should not require a value" do
45
+ @model.should_not validate_presence_of(:attr)
46
+ end
47
+ end
48
+
49
+ context "an optional attribute on a class using ActiveModel::Validations" do
50
+ before do
51
+ @model = define_active_model_class("Example", :accessors => [:attr]).new
52
+ end
53
+
54
+ it "should not require a value" do
55
+ @model.should_not validate_presence_of(:attr)
56
+ end
57
+ end
58
+
59
+ context "a required has_many association" do
60
+ before do
61
+ define_model :child
62
+ @model = define_model :parent do
63
+ has_many :children
64
+ validates_presence_of :children
65
+ end.new
66
+ end
67
+
68
+ it "should require the attribute to be set" do
69
+ @model.should validate_presence_of(:children)
70
+ end
71
+ end
72
+
73
+ context "an optional has_many association" do
74
+ before do
75
+ define_model :child
76
+ @model = define_model :parent do
77
+ has_many :children
78
+ end.new
79
+ end
80
+
81
+ it "should not require the attribute to be set" do
82
+ @model.should_not validate_presence_of(:children)
83
+ end
84
+ end
85
+
86
+ context "a required has_and_belongs_to_many association" do
87
+ before do
88
+ define_model :child
89
+ @model = define_model :parent do
90
+ has_and_belongs_to_many :children
91
+ validates_presence_of :children
92
+ end.new
93
+ create_table "children_parents", :id => false do |t|
94
+ t.integer :child_id
95
+ t.integer :parent_id
96
+ end
97
+ end
98
+
99
+ it "should require the attribute to be set" do
100
+ @model.should validate_presence_of(:children)
101
+ end
102
+ end
103
+
104
+ context "an optional has_and_belongs_to_many association" do
105
+ before do
106
+ define_model :child
107
+ @model = define_model :parent do
108
+ has_and_belongs_to_many :children
109
+ end.new
110
+ create_table "children_parents", :id => false do |t|
111
+ t.integer :child_id
112
+ t.integer :parent_id
113
+ end
114
+ end
115
+
116
+ it "should not require the attribute to be set" do
117
+ @model.should_not validate_presence_of(:children)
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,143 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveModel::ValidateUniquenessOfMatcher do
4
+ context "a unique attribute" do
5
+ before do
6
+ @model = define_model(:example, :attr => :string,
7
+ :other => :integer) do
8
+ validates_uniqueness_of :attr
9
+ end.new
10
+ end
11
+
12
+ context "with an existing value" do
13
+ before do
14
+ @existing = Example.create!(:attr => 'value', :other => 1)
15
+ end
16
+
17
+ it "should require a unique value for that attribute" do
18
+ @model.should validate_uniqueness_of(:attr)
19
+ end
20
+
21
+ it "should pass when the subject is an existing record" do
22
+ @existing.should validate_uniqueness_of(:attr)
23
+ end
24
+
25
+ it "should fail when a scope is specified" do
26
+ @model.should_not validate_uniqueness_of(:attr).scoped_to(:other)
27
+ end
28
+ end
29
+
30
+ context "without an existing value" do
31
+ before do
32
+ Example.find(:first).should be_nil
33
+ @matcher = validate_uniqueness_of(:attr)
34
+ end
35
+
36
+ it "should fail to require a unique value" do
37
+ @model.should_not @matcher
38
+ end
39
+
40
+ it "should alert the tester that an existing value is not present" do
41
+ @matcher.matches?(@model)
42
+ @matcher.negative_failure_message.should =~ /^Can't find first .*/
43
+ end
44
+ end
45
+ end
46
+
47
+ context "a unique attribute with a custom error and an existing value" do
48
+ before do
49
+ @model = define_model(:example, :attr => :string) do
50
+ validates_uniqueness_of :attr, :message => 'Bad value'
51
+ end.new
52
+ Example.create!(:attr => 'value')
53
+ end
54
+
55
+ it "should fail when checking the default message" do
56
+ @model.should_not validate_uniqueness_of(:attr)
57
+ end
58
+
59
+ it "should fail when checking a message that doesn't match" do
60
+ @model.should_not validate_uniqueness_of(:attr).with_message(/abc/i)
61
+ end
62
+
63
+ it "should pass when checking a message that matches" do
64
+ @model.should validate_uniqueness_of(:attr).with_message(/bad/i)
65
+ end
66
+ end
67
+
68
+ context "a scoped unique attribute with an existing value" do
69
+ before do
70
+ @model = define_model(:example, :attr => :string,
71
+ :scope1 => :integer,
72
+ :scope2 => :integer) do
73
+ validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
74
+ end.new
75
+ @existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
76
+ end
77
+
78
+ it "should pass when the correct scope is specified" do
79
+ @model.should validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2)
80
+ end
81
+
82
+ it "should pass when the subject is an existing record" do
83
+ @existing.should validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2)
84
+ end
85
+
86
+ it "should fail when a different scope is specified" do
87
+ @model.should_not validate_uniqueness_of(:attr).scoped_to(:scope1)
88
+ end
89
+
90
+ it "should fail when no scope is specified" do
91
+ @model.should_not validate_uniqueness_of(:attr)
92
+ end
93
+
94
+ it "should fail when a non-existent attribute is specified as a scope" do
95
+ @model.should_not validate_uniqueness_of(:attr).scoped_to(:fake)
96
+ end
97
+ end
98
+
99
+ context "a non-unique attribute with an existing value" do
100
+ before do
101
+ @model = define_model(:example, :attr => :string).new
102
+ Example.create!(:attr => 'value')
103
+ end
104
+
105
+ it "should not require a unique value for that attribute" do
106
+ @model.should_not validate_uniqueness_of(:attr)
107
+ end
108
+ end
109
+
110
+ context "a case sensitive unique attribute with an existing value" do
111
+ before do
112
+ @model = define_model(:example, :attr => :string) do
113
+ validates_uniqueness_of :attr, :case_sensitive => true
114
+ end.new
115
+ Example.create!(:attr => 'value')
116
+ end
117
+
118
+ it "should not require a unique, case-insensitive value for that attribute" do
119
+ @model.should_not validate_uniqueness_of(:attr).case_insensitive
120
+ end
121
+
122
+ it "should require a unique, case-sensitive value for that attribute" do
123
+ @model.should validate_uniqueness_of(:attr)
124
+ end
125
+ end
126
+
127
+ context "a case sensitive unique integer attribute with an existing value" do
128
+ before do
129
+ @model = define_model(:example, :attr => :integer) do
130
+ validates_uniqueness_of :attr, :case_sensitive => true
131
+ end.new
132
+ Example.create!(:attr => 'value')
133
+ end
134
+
135
+ it "should require a unique, case-insensitive value for that attribute" do
136
+ @model.should validate_uniqueness_of(:attr).case_insensitive
137
+ end
138
+
139
+ it "should require a unique, case-sensitive value for that attribute" do
140
+ @model.should validate_uniqueness_of(:attr)
141
+ end
142
+ end
143
+ end