shoulda-matchers 1.4.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +11 -3
- data/Appraisals +3 -3
- data/Gemfile +1 -1
- data/Gemfile.lock +69 -70
- data/MIT-LICENSE +1 -1
- data/NEWS.md +22 -4
- data/README.md +3 -1
- data/Rakefile +9 -5
- data/features/step_definitions/rails_steps.rb +7 -7
- data/features/support/env.rb +1 -1
- data/gemfiles/3.0.gemfile +1 -1
- data/gemfiles/3.0.gemfile.lock +25 -22
- data/gemfiles/3.1.gemfile +1 -1
- data/gemfiles/3.1.gemfile.lock +27 -24
- data/gemfiles/3.2.gemfile +1 -1
- data/gemfiles/3.2.gemfile.lock +56 -52
- data/lib/shoulda/matchers/action_controller.rb +3 -2
- data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +10 -8
- data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +2 -4
- data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +3 -4
- data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +3 -4
- data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
- data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +5 -2
- data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +2 -2
- data/lib/shoulda/matchers/action_controller/route_matcher.rb +12 -12
- data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +3 -4
- data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +10 -11
- data/lib/shoulda/matchers/action_controller/strong_parameters_matcher.rb +121 -0
- data/lib/shoulda/matchers/action_mailer/have_sent_email_matcher.rb +7 -4
- data/lib/shoulda/matchers/active_model.rb +1 -1
- data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +8 -8
- data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +5 -4
- data/lib/shoulda/matchers/active_model/disallow_value_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_model/exception_message_finder.rb +0 -1
- data/lib/shoulda/matchers/active_model/helpers.rb +16 -7
- data/lib/shoulda/matchers/active_model/only_integer_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +8 -6
- data/lib/shoulda/matchers/active_model/validate_format_of_matcher.rb +19 -10
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +4 -4
- data/lib/shoulda/matchers/active_model/validate_uniqueness_of_matcher.rb +19 -21
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +7 -7
- data/lib/shoulda/matchers/active_model/validation_message_finder.rb +1 -1
- data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_record/association_matcher.rb +3 -3
- data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +11 -7
- data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +5 -5
- data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +5 -2
- data/lib/shoulda/matchers/active_record/serialize_matcher.rb +3 -3
- data/lib/shoulda/matchers/assertion_error.rb +4 -1
- data/lib/shoulda/matchers/independent/delegate_matcher.rb +17 -7
- data/lib/shoulda/matchers/integrations/rspec.rb +19 -33
- data/lib/shoulda/matchers/version.rb +1 -1
- data/shoulda-matchers.gemspec +6 -4
- data/spec/shoulda/matchers/action_controller/assign_to_matcher_spec.rb +66 -0
- data/spec/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +22 -0
- data/spec/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +42 -0
- data/spec/shoulda/matchers/action_controller/render_template_matcher_spec.rb +78 -0
- data/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +58 -0
- data/spec/shoulda/matchers/action_controller/respond_with_content_type_matcher_spec.rb +31 -0
- data/spec/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +31 -0
- data/spec/shoulda/matchers/action_controller/route_matcher_spec.rb +65 -0
- data/spec/shoulda/matchers/action_controller/set_session_matcher_spec.rb +51 -0
- data/spec/shoulda/matchers/action_controller/set_the_flash_matcher_spec.rb +153 -0
- data/spec/shoulda/matchers/action_controller/strong_parameters_matcher_spec.rb +142 -0
- data/spec/shoulda/matchers/action_mailer/have_sent_email_spec.rb +324 -0
- data/spec/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +111 -0
- data/spec/shoulda/matchers/active_model/allow_value_matcher_spec.rb +124 -0
- data/spec/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +43 -0
- data/spec/shoulda/matchers/active_model/ensure_exclusion_of_matcher_spec.rb +74 -0
- data/spec/shoulda/matchers/active_model/ensure_inclusion_of_matcher_spec.rb +171 -0
- data/spec/shoulda/matchers/active_model/ensure_length_of_matcher_spec.rb +113 -0
- data/spec/shoulda/{active_model → matchers/active_model}/exception_message_finder_spec.rb +2 -2
- data/spec/shoulda/matchers/active_model/helpers_spec.rb +158 -0
- data/spec/shoulda/matchers/active_model/only_integer_matcher_spec.rb +52 -0
- data/spec/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +41 -0
- data/spec/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +41 -0
- data/spec/shoulda/matchers/active_model/validate_format_of_matcher_spec.rb +75 -0
- data/spec/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +73 -0
- data/spec/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +127 -0
- data/spec/shoulda/matchers/active_model/validate_uniqueness_of_matcher_spec.rb +175 -0
- data/spec/shoulda/{active_model → matchers/active_model}/validation_message_finder_spec.rb +0 -1
- data/spec/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +106 -0
- data/spec/shoulda/matchers/active_record/association_matcher_spec.rb +534 -0
- data/spec/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +111 -0
- data/spec/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +78 -0
- data/spec/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +41 -0
- data/spec/shoulda/{active_record → matchers/active_record}/query_the_database_matcher_spec.rb +10 -10
- data/spec/shoulda/matchers/active_record/serialize_matcher_spec.rb +86 -0
- data/spec/shoulda/{independent → matchers/independent}/delegate_matcher_spec.rb +55 -34
- data/spec/spec_helper.rb +1 -2
- data/spec/support/active_model_versions.rb +4 -0
- data/spec/support/activemodel_helpers.rb +19 -0
- data/spec/support/controller_builder.rb +38 -12
- data/spec/support/model_builder.rb +8 -2
- metadata +106 -98
- data/spec/fixtures/addresses.yml +0 -3
- data/spec/fixtures/friendships.yml +0 -0
- data/spec/fixtures/posts.yml +0 -5
- data/spec/fixtures/products.yml +0 -0
- data/spec/fixtures/taggings.yml +0 -0
- data/spec/fixtures/tags.yml +0 -9
- data/spec/fixtures/users.yml +0 -6
- data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +0 -63
- data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +0 -20
- data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +0 -40
- data/spec/shoulda/action_controller/render_template_matcher_spec.rb +0 -69
- data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +0 -55
- data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +0 -28
- data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +0 -83
- data/spec/shoulda/action_controller/route_matcher_spec.rb +0 -65
- data/spec/shoulda/action_controller/set_session_matcher_spec.rb +0 -46
- data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +0 -130
- data/spec/shoulda/action_mailer/have_sent_email_spec.rb +0 -333
- data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -115
- data/spec/shoulda/active_model/allow_value_matcher_spec.rb +0 -131
- data/spec/shoulda/active_model/disallow_value_matcher_spec.rb +0 -65
- data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +0 -79
- data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +0 -181
- data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +0 -138
- data/spec/shoulda/active_model/helpers_spec.rb +0 -129
- data/spec/shoulda/active_model/only_integer_matcher_spec.rb +0 -69
- data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +0 -43
- data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +0 -48
- data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +0 -79
- data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +0 -112
- data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +0 -135
- data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +0 -154
- data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -84
- data/spec/shoulda/active_record/association_matcher_spec.rb +0 -642
- data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +0 -185
- data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +0 -105
- data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +0 -46
- data/spec/shoulda/active_record/serialize_matcher_spec.rb +0 -81
@@ -1,112 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
|
4
|
-
it "should state in its description that it allows only numeric values" do
|
5
|
-
matcher = Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher.new(:attr)
|
6
|
-
matcher.description.should == "only allow numeric values for attr"
|
7
|
-
end
|
8
|
-
|
9
|
-
context "given a numeric attribute" do
|
10
|
-
before do
|
11
|
-
define_model :example, :attr => :string do
|
12
|
-
validates_numericality_of :attr
|
13
|
-
end
|
14
|
-
@model = Example.new
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should only allow numeric values for that attribute" do
|
18
|
-
matcher = new_matcher(:attr)
|
19
|
-
matcher.matches?(@model).should be_true
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should not override the default message with a blank" do
|
23
|
-
matcher = new_matcher(:attr)
|
24
|
-
matcher.with_message(nil)
|
25
|
-
matcher.matches?(@model).should be_true
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when asked to enforce integer values for that attribute" do
|
29
|
-
it "should not match" do
|
30
|
-
matcher = new_matcher(:attr)
|
31
|
-
matcher.only_integer
|
32
|
-
matcher.matches?(@model).should be_false
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should fail with the ActiveRecord :not_an_integer message" do
|
36
|
-
matcher = new_matcher(:attr)
|
37
|
-
matcher.only_integer
|
38
|
-
matcher.matches?(@model)
|
39
|
-
matcher.failure_message.should include 'Expected errors to include "must be an integer"'
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context "given a numeric attribute which must be integer" do
|
45
|
-
before do
|
46
|
-
define_model :example, :attr => :string do
|
47
|
-
validates_numericality_of :attr, { :only_integer => true }
|
48
|
-
end
|
49
|
-
@model = Example.new
|
50
|
-
end
|
51
|
-
|
52
|
-
it "allows integer values for that attribute" do
|
53
|
-
matcher = new_matcher(:attr)
|
54
|
-
matcher.only_integer
|
55
|
-
matcher.matches?(@model).should be_true
|
56
|
-
end
|
57
|
-
|
58
|
-
it "does not allow non-integer values for that attribute" do
|
59
|
-
matcher = new_matcher(:attr)
|
60
|
-
matcher.only_integer
|
61
|
-
matcher.matches?(@model).should be_true
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should state in its description that it allows only integer values" do
|
65
|
-
matcher = new_matcher(:attr)
|
66
|
-
matcher.only_integer
|
67
|
-
matcher.description.should == "only allow numeric, integer values for attr"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
context "given a numeric attribute with a custom validation message" do
|
72
|
-
before do
|
73
|
-
define_model :example, :attr => :string do
|
74
|
-
validates_numericality_of :attr, :message => 'custom'
|
75
|
-
end
|
76
|
-
@model = Example.new
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should only allow numeric values for that attribute with that message" do
|
80
|
-
matcher = new_matcher(:attr)
|
81
|
-
matcher.with_message(/custom/)
|
82
|
-
matcher.matches?(@model).should be_true
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should not allow numeric values for that attribute with another message" do
|
86
|
-
matcher = new_matcher(:attr)
|
87
|
-
matcher.with_message(/wrong/)
|
88
|
-
matcher.matches?(@model).should be_false
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "given a non-numeric attribute" do
|
93
|
-
before do
|
94
|
-
@model = define_model(:example, :attr => :string).new
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should not only allow numeric values for that attribute" do
|
98
|
-
matcher = new_matcher(:attr)
|
99
|
-
matcher.matches?(@model).should be_false
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should fail with the ActiveRecord :not_a_number message" do
|
103
|
-
matcher = new_matcher(:attr)
|
104
|
-
matcher.matches?(@model)
|
105
|
-
matcher.failure_message.should include 'Expected errors to include "is not a number"'
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
def new_matcher(attr)
|
110
|
-
Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher.new(attr)
|
111
|
-
end
|
112
|
-
end
|
@@ -1,135 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
|
4
|
-
context "a required attribute" do
|
5
|
-
before do
|
6
|
-
define_model :example, :attr => :string do
|
7
|
-
validates_presence_of :attr
|
8
|
-
end
|
9
|
-
@model = Example.new
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should require a value" do
|
13
|
-
@model.should validate_presence_of(:attr)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should not override the default message with a blank" do
|
17
|
-
@model.should validate_presence_of(:attr).with_message(nil)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "a required attribute on a class using ActiveModel::Validations" do
|
22
|
-
before do
|
23
|
-
define_active_model_class("Example", :accessors => [:attr]) do
|
24
|
-
validates_presence_of :attr
|
25
|
-
end
|
26
|
-
@model = Example.new
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should require a value" do
|
30
|
-
@model.should validate_presence_of(:attr)
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should not override the default message with a blank" do
|
34
|
-
@model.should validate_presence_of(:attr).with_message(nil)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context "an optional attribute" do
|
39
|
-
before do
|
40
|
-
@model = define_model(:example, :attr => :string).new
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should not require a value" do
|
44
|
-
@model.should_not validate_presence_of(:attr)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "an optional attribute on a class using ActiveModel::Validations" do
|
49
|
-
before do
|
50
|
-
@model = define_active_model_class("Example", :accessors => [:attr]).new
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should not require a value" do
|
54
|
-
@model.should_not validate_presence_of(:attr)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context "a required has_many association" do
|
59
|
-
before do
|
60
|
-
define_model :child
|
61
|
-
@model = define_model :parent do
|
62
|
-
has_many :children
|
63
|
-
validates_presence_of :children
|
64
|
-
end.new
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should require the attribute to be set" do
|
68
|
-
@model.should validate_presence_of(:children)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
context "an optional has_many association" do
|
73
|
-
before do
|
74
|
-
define_model :child
|
75
|
-
@model = define_model :parent do
|
76
|
-
has_many :children
|
77
|
-
end.new
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should not require the attribute to be set" do
|
81
|
-
@model.should_not validate_presence_of(:children)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context "a required has_and_belongs_to_many association" do
|
86
|
-
before do
|
87
|
-
define_model :child
|
88
|
-
@model = define_model :parent do
|
89
|
-
has_and_belongs_to_many :children
|
90
|
-
validates_presence_of :children
|
91
|
-
end.new
|
92
|
-
create_table "children_parents", :id => false do |t|
|
93
|
-
t.integer :child_id
|
94
|
-
t.integer :parent_id
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should require the attribute to be set" do
|
99
|
-
@model.should validate_presence_of(:children)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context "an optional has_and_belongs_to_many association" do
|
104
|
-
before do
|
105
|
-
define_model :child
|
106
|
-
@model = define_model :parent do
|
107
|
-
has_and_belongs_to_many :children
|
108
|
-
end.new
|
109
|
-
create_table "children_parents", :id => false do |t|
|
110
|
-
t.integer :child_id
|
111
|
-
t.integer :parent_id
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
it "should not require the attribute to be set" do
|
116
|
-
@model.should_not validate_presence_of(:children)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
if Rails::VERSION::STRING.to_f >= 3.2
|
121
|
-
context "a strictly required attribute" do
|
122
|
-
before do
|
123
|
-
define_model :example, :attr => :string do
|
124
|
-
validates_presence_of :attr, :strict => true
|
125
|
-
end
|
126
|
-
@model = Example.new
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should require a value" do
|
130
|
-
@model.should validate_presence_of(:attr).strict
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
@@ -1,154 +0,0 @@
|
|
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
|
-
attr_accessible :attr, :other
|
9
|
-
validates_uniqueness_of :attr
|
10
|
-
end.new
|
11
|
-
end
|
12
|
-
|
13
|
-
context "with an existing value" do
|
14
|
-
before do
|
15
|
-
@existing = Example.create!(:attr => 'value', :other => 1)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should require a unique value for that attribute" do
|
19
|
-
@model.should validate_uniqueness_of(:attr)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should pass when the subject is an existing record" do
|
23
|
-
@existing.should validate_uniqueness_of(:attr)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "should fail when a scope is specified" do
|
27
|
-
@model.should_not validate_uniqueness_of(:attr).scoped_to(:other)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "without an existing value" do
|
32
|
-
before do
|
33
|
-
Example.find(:first).should be_nil
|
34
|
-
@matcher = validate_uniqueness_of(:attr)
|
35
|
-
end
|
36
|
-
|
37
|
-
it "does not not require a created instance" do
|
38
|
-
@model.should @matcher
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "a unique attribute with a custom error and an existing value" do
|
44
|
-
before do
|
45
|
-
@model = define_model(:example, :attr => :string) do
|
46
|
-
attr_accessible :attr
|
47
|
-
validates_uniqueness_of :attr, :message => 'Bad value'
|
48
|
-
end.new
|
49
|
-
Example.create!(:attr => 'value')
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should fail when checking the default message" do
|
53
|
-
@model.should_not validate_uniqueness_of(:attr)
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should fail when checking a message that doesn't match" do
|
57
|
-
@model.should_not validate_uniqueness_of(:attr).with_message(/abc/i)
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should pass when checking a message that matches" do
|
61
|
-
@model.should validate_uniqueness_of(:attr).with_message(/bad/i)
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context "a scoped unique attribute with an existing value" do
|
66
|
-
before do
|
67
|
-
@model = define_model(:example, :attr => :string,
|
68
|
-
:scope1 => :integer,
|
69
|
-
:scope2 => :integer,
|
70
|
-
:other => :integer) do
|
71
|
-
attr_accessible :attr, :scope1, :scope2, :other
|
72
|
-
validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
|
73
|
-
end.new
|
74
|
-
@existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2, :other => 3)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should pass when the correct scope is specified" do
|
78
|
-
@model.should validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should pass when the subject is an existing record" do
|
82
|
-
@existing.should validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should fail when too narrow of a scope is specified" do
|
86
|
-
@model.should_not validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2, :other)
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should fail when too broad of a scope is specified" do
|
90
|
-
@model.should_not validate_uniqueness_of(:attr).scoped_to(:scope1)
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should fail when a different scope is specified" do
|
94
|
-
@model.should_not validate_uniqueness_of(:attr).scoped_to(:other)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should fail when no scope is specified" do
|
98
|
-
@model.should_not validate_uniqueness_of(:attr)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should fail when a non-existent attribute is specified as a scope" do
|
102
|
-
@model.should_not validate_uniqueness_of(:attr).scoped_to(:fake)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
context "a non-unique attribute with an existing value" do
|
107
|
-
before do
|
108
|
-
@model = define_model(:example, :attr => :string) do
|
109
|
-
attr_accessible :attr
|
110
|
-
end.new
|
111
|
-
Example.create!(:attr => 'value')
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should not require a unique value for that attribute" do
|
115
|
-
@model.should_not validate_uniqueness_of(:attr)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
context "a case sensitive unique attribute with an existing value" do
|
120
|
-
before do
|
121
|
-
@model = define_model(:example, :attr => :string) do
|
122
|
-
attr_accessible :attr
|
123
|
-
validates_uniqueness_of :attr, :case_sensitive => true
|
124
|
-
end.new
|
125
|
-
Example.create!(:attr => 'value')
|
126
|
-
end
|
127
|
-
|
128
|
-
it "should not require a unique, case-insensitive value for that attribute" do
|
129
|
-
@model.should_not validate_uniqueness_of(:attr).case_insensitive
|
130
|
-
end
|
131
|
-
|
132
|
-
it "should require a unique, case-sensitive value for that attribute" do
|
133
|
-
@model.should validate_uniqueness_of(:attr)
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
context "a case sensitive unique integer attribute with an existing value" do
|
138
|
-
before do
|
139
|
-
@model = define_model(:example, :attr => :integer) do
|
140
|
-
attr_accessible :attr
|
141
|
-
validates_uniqueness_of :attr, :case_sensitive => true
|
142
|
-
end.new
|
143
|
-
Example.create!(:attr => 'value')
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should require a unique, case-insensitive value for that attribute" do
|
147
|
-
@model.should validate_uniqueness_of(:attr).case_insensitive
|
148
|
-
end
|
149
|
-
|
150
|
-
it "should require a unique, case-sensitive value for that attribute" do
|
151
|
-
@model.should validate_uniqueness_of(:attr)
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
@@ -1,84 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveRecord::AcceptNestedAttributesForMatcher do
|
4
|
-
before do
|
5
|
-
define_model :child, :parent_id => :integer
|
6
|
-
define_model :parent do
|
7
|
-
has_many :children
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:parent) { Parent.new }
|
12
|
-
let(:matcher) { accept_nested_attributes_for(:children) }
|
13
|
-
|
14
|
-
it "should accept an existing declaration" do
|
15
|
-
Parent.accepts_nested_attributes_for :children
|
16
|
-
matcher.matches?(parent).should be_true
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should reject a missing declaration" do
|
20
|
-
matcher.matches?(parent).should be_false
|
21
|
-
matcher.failure_message.should == "Expected Parent to accept nested attributes for children (is not declared)"
|
22
|
-
end
|
23
|
-
|
24
|
-
context "allow_destroy" do
|
25
|
-
it "should accept a valid truthy value" do
|
26
|
-
Parent.accepts_nested_attributes_for :children, :allow_destroy => true
|
27
|
-
matcher.allow_destroy(true).matches?(parent).should be_true
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should accept a valid falsey value" do
|
31
|
-
Parent.accepts_nested_attributes_for :children, :allow_destroy => false
|
32
|
-
matcher.allow_destroy(false).matches?(parent).should be_true
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should reject an invalid truthy value" do
|
36
|
-
Parent.accepts_nested_attributes_for :children, :allow_destroy => true
|
37
|
-
matcher.allow_destroy(false).matches?(parent).should be_false
|
38
|
-
matcher.failure_message.should =~ /should not allow destroy/
|
39
|
-
end
|
40
|
-
|
41
|
-
it "should reject an invalid falsey value" do
|
42
|
-
Parent.accepts_nested_attributes_for :children, :allow_destroy => false
|
43
|
-
matcher.allow_destroy(true).matches?(parent).should be_false
|
44
|
-
matcher.failure_message.should =~ /should allow destroy/
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context "limit" do
|
49
|
-
it "should accept a correct value" do
|
50
|
-
Parent.accepts_nested_attributes_for :children, :limit => 3
|
51
|
-
matcher.limit(3).matches?(parent).should be_true
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should reject a false value" do
|
55
|
-
Parent.accepts_nested_attributes_for :children, :limit => 3
|
56
|
-
matcher.limit(2).matches?(parent).should be_false
|
57
|
-
matcher.failure_message.should =~ /limit should be 2, got 3/
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
context "update_only" do
|
62
|
-
it "should accept a valid truthy value" do
|
63
|
-
Parent.accepts_nested_attributes_for :children, :update_only => true
|
64
|
-
matcher.update_only(true).matches?(parent).should be_true
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should accept a valid falsey value" do
|
68
|
-
Parent.accepts_nested_attributes_for :children, :update_only => false
|
69
|
-
matcher.update_only(false).matches?(parent).should be_true
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should reject an invalid truthy value" do
|
73
|
-
Parent.accepts_nested_attributes_for :children, :update_only => true
|
74
|
-
matcher.update_only(false).matches?(parent).should be_false
|
75
|
-
matcher.failure_message.should =~ /should not be update only/
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should reject an invalid falsey value" do
|
79
|
-
Parent.accepts_nested_attributes_for :children, :update_only => false
|
80
|
-
matcher.update_only(true).matches?(parent).should be_false
|
81
|
-
matcher.failure_message.should =~ /should be update only/
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|