shoulda-matchers 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/.travis.yml +11 -3
  2. data/Appraisals +3 -3
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +69 -70
  5. data/MIT-LICENSE +1 -1
  6. data/NEWS.md +22 -4
  7. data/README.md +3 -1
  8. data/Rakefile +9 -5
  9. data/features/step_definitions/rails_steps.rb +7 -7
  10. data/features/support/env.rb +1 -1
  11. data/gemfiles/3.0.gemfile +1 -1
  12. data/gemfiles/3.0.gemfile.lock +25 -22
  13. data/gemfiles/3.1.gemfile +1 -1
  14. data/gemfiles/3.1.gemfile.lock +27 -24
  15. data/gemfiles/3.2.gemfile +1 -1
  16. data/gemfiles/3.2.gemfile.lock +56 -52
  17. data/lib/shoulda/matchers/action_controller.rb +3 -2
  18. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +10 -8
  19. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +2 -4
  20. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +3 -4
  21. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +3 -4
  22. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
  23. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +5 -2
  24. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +2 -2
  25. data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
  26. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +3 -4
  27. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +10 -11
  28. data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +121 -0
  29. data/lib/shoulda/matchers/action_mailer/have_sent_email_matcher.rb +7 -4
  30. data/lib/shoulda/matchers/active_model.rb +1 -1
  31. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +8 -8
  32. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +5 -4
  33. data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +3 -3
  34. data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +2 -2
  35. data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -1
  36. data/lib/shoulda/matchers/active_model/helpers.rb +16 -7
  37. data/lib/shoulda/matchers/active_model/only_integer_matcher.rb +3 -3
  38. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +8 -6
  39. data/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +19 -10
  40. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +4 -4
  41. data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +19 -21
  42. data/lib/shoulda/matchers/active_model/validation_matcher.rb +7 -7
  43. data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -1
  44. data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +5 -5
  45. data/lib/shoulda/matchers/active_record/association_matcher.rb +3 -3
  46. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +11 -7
  47. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +2 -2
  48. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +5 -5
  49. data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +5 -2
  50. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +3 -3
  51. data/lib/shoulda/matchers/assertion_error.rb +4 -1
  52. data/lib/shoulda/matchers/independent/delegate_matcher.rb +17 -7
  53. data/lib/shoulda/matchers/integrations/rspec.rb +19 -33
  54. data/lib/shoulda/matchers/version.rb +1 -1
  55. data/shoulda-matchers.gemspec +6 -4
  56. data/spec/shoulda/matchers/action_controller/assign_to_matcher_spec.rb +66 -0
  57. data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +22 -0
  58. data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +42 -0
  59. data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +78 -0
  60. data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +58 -0
  61. data/spec/shoulda/matchers/action_controller/respond_with_content_type_matcher_spec.rb +31 -0
  62. data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +31 -0
  63. data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +65 -0
  64. data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +51 -0
  65. data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +153 -0
  66. data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +142 -0
  67. data/spec/shoulda/matchers/action_mailer/have_sent_email_spec.rb +324 -0
  68. data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +111 -0
  69. data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +124 -0
  70. data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +43 -0
  71. data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +74 -0
  72. data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +171 -0
  73. data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +113 -0
  74. data/spec/shoulda/{active_model → matchers/active_model}/exception_message_finder_spec.rb +2 -2
  75. data/spec/shoulda/matchers/active_model/helpers_spec.rb +158 -0
  76. data/spec/shoulda/matchers/active_model/only_integer_matcher_spec.rb +52 -0
  77. data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +41 -0
  78. data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +41 -0
  79. data/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb +75 -0
  80. data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +73 -0
  81. data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +127 -0
  82. data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +175 -0
  83. data/spec/shoulda/{active_model → matchers/active_model}/validation_message_finder_spec.rb +0 -1
  84. data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +106 -0
  85. data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +534 -0
  86. data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +111 -0
  87. data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +78 -0
  88. data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +41 -0
  89. data/spec/shoulda/{active_record → matchers/active_record}/query_the_database_matcher_spec.rb +10 -10
  90. data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +86 -0
  91. data/spec/shoulda/{independent → matchers/independent}/delegate_matcher_spec.rb +55 -34
  92. data/spec/spec_helper.rb +1 -2
  93. data/spec/support/active_model_versions.rb +4 -0
  94. data/spec/support/activemodel_helpers.rb +19 -0
  95. data/spec/support/controller_builder.rb +38 -12
  96. data/spec/support/model_builder.rb +8 -2
  97. metadata +106 -98
  98. data/spec/fixtures/addresses.yml +0 -3
  99. data/spec/fixtures/friendships.yml +0 -0
  100. data/spec/fixtures/posts.yml +0 -5
  101. data/spec/fixtures/products.yml +0 -0
  102. data/spec/fixtures/taggings.yml +0 -0
  103. data/spec/fixtures/tags.yml +0 -9
  104. data/spec/fixtures/users.yml +0 -6
  105. data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +0 -63
  106. data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +0 -20
  107. data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +0 -40
  108. data/spec/shoulda/action_controller/render_template_matcher_spec.rb +0 -69
  109. data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +0 -55
  110. data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +0 -28
  111. data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +0 -83
  112. data/spec/shoulda/action_controller/route_matcher_spec.rb +0 -65
  113. data/spec/shoulda/action_controller/set_session_matcher_spec.rb +0 -46
  114. data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +0 -130
  115. data/spec/shoulda/action_mailer/have_sent_email_spec.rb +0 -333
  116. data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -115
  117. data/spec/shoulda/active_model/allow_value_matcher_spec.rb +0 -131
  118. data/spec/shoulda/active_model/disallow_value_matcher_spec.rb +0 -65
  119. data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +0 -79
  120. data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +0 -181
  121. data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +0 -138
  122. data/spec/shoulda/active_model/helpers_spec.rb +0 -129
  123. data/spec/shoulda/active_model/only_integer_matcher_spec.rb +0 -69
  124. data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +0 -43
  125. data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +0 -48
  126. data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +0 -79
  127. data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +0 -112
  128. data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +0 -135
  129. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +0 -154
  130. data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -84
  131. data/spec/shoulda/active_record/association_matcher_spec.rb +0 -642
  132. data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +0 -185
  133. data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +0 -105
  134. data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +0 -46
  135. data/spec/shoulda/active_record/serialize_matcher_spec.rb +0 -81
@@ -1,115 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher do
4
- describe "#description" do
5
- context 'without a role' do
6
- it 'includes the attribute name' do
7
- matcher = Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher.new(:attr)
8
- matcher.description.should eq("allow mass assignment of attr")
9
- end
10
- end
11
-
12
- if active_model_3_1?
13
- context 'with a role' do
14
- it 'includes the attribute name and the role' do
15
- matcher = Shoulda::Matchers::ActiveModel::AllowMassAssignmentOfMatcher.new(:attr).as(:admin)
16
- matcher.description.should eq("allow mass assignment of attr as admin")
17
- end
18
- end
19
- end
20
- end
21
-
22
- context "an attribute that is blacklisted from mass-assignment" do
23
- let(:model) do
24
- define_model(:example, :attr => :string) do
25
- attr_protected :attr
26
- end.new
27
- end
28
-
29
- it "should reject being mass-assignable" do
30
- model.should_not allow_mass_assignment_of(:attr)
31
- end
32
- end
33
-
34
- context "an attribute that is not whitelisted for mass-assignment" do
35
- let(:model) do
36
- define_model(:example, :attr => :string, :other => :string) do
37
- attr_accessible :other
38
- end.new
39
- end
40
-
41
- it "should reject being mass-assignable" do
42
- model.should_not allow_mass_assignment_of(:attr)
43
- end
44
- end
45
-
46
- context "an attribute that is whitelisted for mass-assignment" do
47
- let(:model) do
48
- define_model(:example, :attr => :string) do
49
- attr_accessible :attr
50
- end.new
51
- end
52
-
53
- it "should accept being mass-assignable" do
54
- model.should allow_mass_assignment_of(:attr)
55
- end
56
- end
57
-
58
- context "an attribute not included in the mass-assignment blacklist" do
59
- let(:model) do
60
- define_model(:example, :attr => :string, :other => :string) do
61
- attr_protected :other
62
- end.new
63
- end
64
-
65
- it "should accept being mass-assignable" do
66
- model.should allow_mass_assignment_of(:attr)
67
- end
68
- end
69
-
70
- unless active_model_3_2?
71
- context "an attribute on a class with no protected attributes" do
72
- let(:model) { define_model(:example, :attr => :string).new }
73
-
74
- it "should accept being mass-assignable" do
75
- model.should allow_mass_assignment_of(:attr)
76
- end
77
-
78
- it "should assign a negative failure message" do
79
- matcher = allow_mass_assignment_of(:attr)
80
- matcher.matches?(model).should == true
81
- matcher.negative_failure_message.should_not be_nil
82
- end
83
- end
84
- end
85
-
86
- context "an attribute on a class with all protected attributes" do
87
- let(:model) do
88
- define_model(:example, :attr => :string) do
89
- attr_accessible
90
- end.new
91
- end
92
-
93
- it "should reject being mass-assignable" do
94
- model.should_not allow_mass_assignment_of(:attr)
95
- end
96
- end
97
-
98
- if active_model_3_1?
99
- context "an attribute included in the mass-assignment whitelist for admin role only" do
100
- let(:model) do
101
- define_model(:example, :attr => :string) do
102
- attr_accessible :attr, :as => :admin
103
- end.new
104
- end
105
-
106
- it "should reject being mass-assignable" do
107
- model.should_not allow_mass_assignment_of(:attr)
108
- end
109
-
110
- it "should accept being mass-assignable for admin" do
111
- model.should allow_mass_assignment_of(:attr).as(:admin)
112
- end
113
- end
114
- end
115
- end
@@ -1,131 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
4
- context "an attribute with a format validation" do
5
- let(:model) do
6
- define_model :example, :attr => :string do
7
- validates_format_of :attr, :with => /abc/
8
- end.new
9
- end
10
-
11
- it "allows a good value" do
12
- model.should allow_value("abcde").for(:attr)
13
- end
14
-
15
- it "rejects a bad value" do
16
- model.should_not allow_value("xyz").for(:attr)
17
- end
18
-
19
- it "allows several good values" do
20
- model.should allow_value("abcde", "deabc").for(:attr)
21
- end
22
-
23
- it "rejects several bad values" do
24
- model.should_not allow_value("xyz", "zyx", nil, []).for(:attr)
25
- end
26
- end
27
-
28
- context "an attribute with a format validation and a custom message" do
29
- let(:model) do
30
- define_model :example, :attr => :string do
31
- validates_format_of :attr, :with => /abc/, :message => 'bad value'
32
- end.new
33
- end
34
-
35
- it "allows a good value" do
36
- model.should allow_value('abcde').for(:attr).with_message(/bad/)
37
- end
38
-
39
- it "rejects a bad value" do
40
- model.should_not allow_value('xyz').for(:attr).with_message(/bad/)
41
- end
42
- end
43
-
44
- context "an attribute with several validations" do
45
- let(:model) do
46
- define_model :example, :attr => :string do
47
- validates_presence_of :attr
48
- validates_length_of :attr, :within => 1..5
49
- validates_numericality_of :attr, :greater_than_or_equal_to => 1,
50
- :less_than_or_equal_to => 50000
51
- end.new
52
- end
53
- bad_values = [nil, "", "abc", "0", "50001", "123456", []]
54
-
55
- it "allows a good value" do
56
- model.should allow_value("12345").for(:attr)
57
- end
58
-
59
- bad_values.each do |bad_value|
60
- it "rejects a bad value (#{bad_value.inspect})" do
61
- model.should_not allow_value(bad_value).for(:attr)
62
- end
63
- end
64
-
65
- it "rejects bad values (#{bad_values.map(&:inspect).join(', ')})" do
66
- model.should_not allow_value(*bad_values).for(:attr)
67
- end
68
- end
69
-
70
- context "an AllowValueMatcher with multiple values" do
71
- it "should describe itself" do
72
- matcher = allow_value("foo", "bar").for(:baz)
73
- matcher.description.should == 'allow baz to be set to any of ["foo", "bar"]'
74
- end
75
- end
76
-
77
- context "an AllowValueMatcher with a single value" do
78
- it "should describe itself" do
79
- matcher = allow_value("foo").for(:baz)
80
- matcher.description.should eq('allow baz to be set to "foo"')
81
- end
82
-
83
- it "allows you to call description before calling matches?" do
84
- model = define_model(:example, :attr => :string).new
85
- matcher = Shoulda::Matchers::ActiveModel::AllowValueMatcher.new("foo").for(:attr)
86
- matcher.description
87
-
88
- expect { matcher.matches?(model) }.not_to raise_error
89
- end
90
- end
91
-
92
- context "an AllowValueMatcher with no values" do
93
- it "raises an error" do
94
- lambda do
95
- allow_value.for(:baz)
96
- end.should raise_error(ArgumentError, /at least one argument/)
97
- end
98
- end
99
-
100
- if Rails::VERSION::STRING.to_f >= 3.2
101
- context "an attribute with a strict format validation" do
102
- let(:model) do
103
- define_model :example, :attr => :string do
104
- validates_format_of :attr, :with => /abc/, :strict => true
105
- end.new
106
- end
107
-
108
- it "strictly rejects a bad value" do
109
- model.should_not allow_value("xyz").for(:attr).strict
110
- end
111
-
112
- it "strictly allows a bad value with a different message" do
113
- model.should allow_value("xyz").for(:attr).with_message(/abc/).strict
114
- end
115
-
116
- it "describes itself" do
117
- allow_value("xyz").for(:attr).strict.description.
118
- should == %{doesn't raise when attr is set to "xyz"}
119
- end
120
-
121
- it "provides a useful negative failure message" do
122
- matcher = allow_value("xyz").for(:attr).strict.with_message(/abc/)
123
- matcher.matches?(model)
124
- matcher.negative_failure_message.
125
- should == 'Expected exception to include /abc/ ' +
126
- 'when attr is set to "xyz", got Attr is invalid'
127
- end
128
- end
129
- end
130
-
131
- end
@@ -1,65 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveModel::DisallowValueMatcher do
4
- it "does not allow any types" do
5
- matcher = Shoulda::Matchers::ActiveModel::DisallowValueMatcher.new("abcde")
6
- matcher.allowed_types.should == ""
7
- end
8
-
9
- context "an attribute with a format validation" do
10
- before do
11
- define_model :example, :attr => :string do
12
- validates_format_of :attr, :with => /abc/
13
- end
14
- @model = Example.new
15
- end
16
-
17
- it "does not match if the value is allowed" do
18
- matcher = new_matcher("abcde")
19
- matcher.for(:attr)
20
- matcher.matches?(@model).should be_false
21
- end
22
-
23
- it "matches if the value is not allowed" do
24
- matcher = new_matcher("xyz")
25
- matcher.for(:attr)
26
- matcher.matches?(@model).should be_true
27
- end
28
- end
29
-
30
- context "an attribute with a format validation and a custom message" do
31
- before do
32
- define_model :example, :attr => :string do
33
- validates_format_of :attr, :with => /abc/, :message => 'good message'
34
- end
35
- @model = Example.new
36
- end
37
-
38
- it "does not match if the value and message are both correct" do
39
- matcher = new_matcher("abcde")
40
- matcher.for(:attr).with_message('good message')
41
- matcher.matches?(@model).should be_false
42
- end
43
-
44
- it "delegates its failure message to its allow matcher's negative failure message" do
45
- allow_matcher = stub_everything(:negative_failure_message => "allow matcher failure")
46
- Shoulda::Matchers::ActiveModel::AllowValueMatcher.stubs(:new).returns(allow_matcher)
47
-
48
- matcher = new_matcher("abcde")
49
- matcher.for(:attr).with_message('good message')
50
- matcher.matches?(@model)
51
-
52
- matcher.failure_message.should == "allow matcher failure"
53
- end
54
-
55
- it "matches if the message is correct but the value is not" do
56
- matcher = new_matcher("xyz")
57
- matcher.for(:attr).with_message('good message')
58
- matcher.matches?(@model).should be_true
59
- end
60
- end
61
-
62
- def new_matcher(value)
63
- matcher = Shoulda::Matchers::ActiveModel::DisallowValueMatcher.new(value)
64
- end
65
- end
@@ -1,79 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveModel::EnsureExclusionOfMatcher do
4
-
5
- context "an attribute which must be excluded of a range" do
6
- before do
7
- @model = define_model(:example, :attr => :integer) do
8
- validates_exclusion_of :attr, :in => 2..5
9
- end.new
10
- end
11
-
12
- it "should accept ensuring the correct range" do
13
- @model.should ensure_exclusion_of(:attr).in_range(2..5)
14
- end
15
-
16
- it "should reject ensuring excluded value" do
17
- @model.should_not ensure_exclusion_of(:attr).in_range(2..6)
18
- end
19
-
20
- it "should not override the default message with a blank" do
21
- @model.should ensure_exclusion_of(:attr).in_range(2..5).with_message(nil)
22
- end
23
- end
24
-
25
- context "an attribute with a custom ranged value validation" do
26
- before do
27
- @model = define_model(:example, :attr => :string) do
28
- validates_exclusion_of :attr, :in => 2..4, :message => 'not good'
29
-
30
- end.new
31
- end
32
-
33
- it "should accept ensuring the correct range" do
34
- @model.should ensure_exclusion_of(:attr).in_range(2..4).with_message(/not good/)
35
- end
36
- end
37
-
38
- context "an attribute with custom range validations" do
39
- before do
40
- define_model :example, :attr => :integer do
41
- validate :custom_validation
42
- def custom_validation
43
- if attr >= 2 && attr <= 5
44
- errors.add(:attr, 'shoud be out of this range')
45
- end
46
- end
47
- end
48
- @model = Example.new
49
- end
50
-
51
- it "should accept ensuring the correct range and messages" do
52
- @model.should ensure_exclusion_of(:attr).in_range(2..5).with_message(/shoud be out of this range/)
53
- end
54
- end
55
-
56
- context "an attribute which must be excluded in an array" do
57
- before do
58
- @model = define_model(:example, :attr => :string) do
59
- validates_exclusion_of :attr, :in => %w(one two)
60
- end.new
61
- end
62
-
63
- it "accepts with correct array" do
64
- @model.should ensure_exclusion_of(:attr).in_array(%w(one two))
65
- end
66
-
67
- it "rejects when only part of array matches" do
68
- @model.should_not ensure_exclusion_of(:attr).in_array(%w(one wrong_value))
69
- end
70
-
71
- it "rejects when array doesn't match at all" do
72
- @model.should_not ensure_exclusion_of(:attr).in_array(%w(cat dog))
73
- end
74
-
75
- it "has correct description" do
76
- ensure_exclusion_of(:attr).in_array([true, 'dog']).description.should == 'ensure exclusion of attr in [true, "dog"]'
77
- end
78
- end
79
- end
@@ -1,181 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
4
- context "with no validations" do
5
- before do
6
- @model = define_model(:example, :attr => :string) do
7
- end.new
8
- end
9
-
10
- it "should reject an array which does not have a validator defined" do
11
- @model.should_not ensure_inclusion_of(:attr).in_array(%w(Yes No))
12
- end
13
- end
14
-
15
- context "with true/false values" do
16
- it "can verify outside values to ensure the negative case" do
17
- model = define_model(:example, :attr => :string).new
18
-
19
- model.should_not ensure_inclusion_of(:attr).in_array([true, false])
20
- end
21
- end
22
-
23
- context "where we cannot determine a value outside the array" do
24
- it "should raise a custom exception" do
25
- model = define_model(:example, :attr => :string).new
26
-
27
- arbitrary_string = Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher::ARBITRARY_OUTSIDE_STRING
28
- expect { model.should ensure_inclusion_of(:attr).in_array([arbitrary_string]) }.to raise_error Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
29
- end
30
- end
31
-
32
- context "an attribute which must be included in a range" do
33
- before do
34
- @model = define_model(:example, :attr => :integer) do
35
- validates_inclusion_of :attr, :in => 2..5
36
- end.new
37
- end
38
-
39
- it "should accept ensuring the correct range" do
40
- @model.should ensure_inclusion_of(:attr).in_range(2..5)
41
- end
42
-
43
- it "should reject ensuring a lower minimum value" do
44
- @model.should_not ensure_inclusion_of(:attr).in_range(1..5)
45
- end
46
-
47
- it "should reject ensuring a higher minimum value" do
48
- @model.should_not ensure_inclusion_of(:attr).in_range(3..5)
49
- end
50
-
51
- it "should reject ensuring a lower maximum value" do
52
- @model.should_not ensure_inclusion_of(:attr).in_range(2..4)
53
- end
54
-
55
- it "should reject ensuring a higher maximum value" do
56
- @model.should_not ensure_inclusion_of(:attr).in_range(2..6)
57
- end
58
-
59
- it "should not override the default message with a blank" do
60
- @model.should ensure_inclusion_of(:attr).in_range(2..5).with_message(nil)
61
- end
62
- end
63
-
64
- context "an attribute with a custom ranged value validation" do
65
- before do
66
- @model = define_model(:example, :attr => :string) do
67
- validates_inclusion_of :attr, :in => 2..4, :message => 'not good'
68
-
69
- end.new
70
- end
71
-
72
- it "should accept ensuring the correct range" do
73
- @model.should ensure_inclusion_of(:attr).in_range(2..4).with_message(/not good/)
74
- end
75
- end
76
-
77
- context "an attribute with custom range validations" do
78
- before do
79
- define_model :example, :attr => :integer do
80
- validate :custom_validation
81
- def custom_validation
82
- if attr < 2
83
- errors.add(:attr, 'too low')
84
- elsif attr > 5
85
- errors.add(:attr, 'too high')
86
- end
87
- end
88
- end
89
- @model = Example.new
90
- end
91
-
92
- it "should accept ensuring the correct range and messages" do
93
- @model.should ensure_inclusion_of(:attr).in_range(2..5).with_low_message(/low/).with_high_message(/high/)
94
- end
95
- end
96
-
97
- context "an attribute which must be included in an array" do
98
- before do
99
- @model = define_model(:example, :attr => :string) do
100
- validates_inclusion_of :attr, :in => %w(one two)
101
- end.new
102
- end
103
-
104
- it "accepts with correct array" do
105
- @model.should ensure_inclusion_of(:attr).in_array(%w(one two))
106
- end
107
-
108
- it "rejects when only part of array matches" do
109
- @model.should_not ensure_inclusion_of(:attr).in_array(%w(one wrong_value))
110
- end
111
-
112
- it "rejects when array doesn't match at all" do
113
- @model.should_not ensure_inclusion_of(:attr).in_array(%w(cat dog))
114
- end
115
-
116
- it "has correct description" do
117
- ensure_inclusion_of(:attr).in_array([true, 'dog']).description.should == 'ensure inclusion of attr in [true, "dog"]'
118
- end
119
-
120
- it "rejects allow_blank" do
121
- @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank(true)
122
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank(false)
123
- end
124
-
125
- it "rejects allow_nil" do
126
- @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(true)
127
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(false)
128
- end
129
- end
130
-
131
- context "allowed blank and allowed nil" do
132
- before do
133
- @model = define_model(:example, :attr => :string) do
134
- validates_inclusion_of :attr, :in => %w(one two), :allow_blank => true, :allow_nil => true
135
- end.new
136
- end
137
-
138
- it "allows allow_blank" do
139
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank(true)
140
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank()
141
- @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank(false)
142
- end
143
-
144
- it "allows allow_nil" do
145
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(true)
146
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil()
147
- @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(false)
148
- end
149
- end
150
-
151
- context "an attribute allowing some blank values but not others" do
152
- before do
153
- @model = define_model(:example, :attr => :string) do
154
- validates_inclusion_of :attr, :in => ['one', 'two', '']
155
- end.new
156
- end
157
-
158
- it "rejects allow_blank" do
159
- @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two', '']).allow_blank(true)
160
- @model.should ensure_inclusion_of(:attr).in_array(['one', 'two', '']).allow_blank(false)
161
- end
162
- end
163
-
164
- if Rails::VERSION::STRING.to_f >= 3.2
165
- context "a strict attribute which must be included in a range" do
166
- before do
167
- @model = define_model(:example, :attr => :integer) do
168
- validates_inclusion_of :attr, :in => 2..5, :strict => true
169
- end.new
170
- end
171
-
172
- it "should accept ensuring the correct range" do
173
- @model.should ensure_inclusion_of(:attr).in_range(2..5).strict
174
- end
175
-
176
- it "should not accept ensuring another range" do
177
- @model.should_not ensure_inclusion_of(:attr).in_range(2..6).strict
178
- end
179
- end
180
- end
181
- end