ratnikov-shoulda 2.0.6.3 → 2.9.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/README.rdoc +3 -2
- data/Rakefile +1 -1
- data/lib/shoulda/active_record/assertions.rb +10 -31
- data/lib/shoulda/active_record/helpers.rb +40 -0
- data/lib/shoulda/active_record/macros.rb +171 -325
- data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
- data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
- data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
- data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
- data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
- data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
- data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
- data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
- data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
- data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
- data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
- data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
- data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
- data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
- data/lib/shoulda/active_record/matchers.rb +42 -0
- data/lib/shoulda/active_record.rb +4 -0
- data/lib/shoulda/assertions.rb +12 -0
- data/lib/shoulda/autoload_macros.rb +46 -0
- data/lib/shoulda/rails.rb +1 -8
- data/lib/shoulda/rspec.rb +5 -0
- data/lib/shoulda/tasks/list_tests.rake +6 -1
- data/lib/shoulda/test_unit.rb +19 -0
- data/lib/shoulda.rb +5 -17
- data/rails/init.rb +1 -1
- data/test/README +2 -2
- data/test/fail_macros.rb +2 -2
- data/test/matchers/allow_mass_assignment_of_matcher_test.rb +68 -0
- data/test/matchers/allow_value_matcher_test.rb +41 -0
- data/test/matchers/association_matcher_test.rb +258 -0
- data/test/matchers/ensure_inclusion_of_matcher_test.rb +80 -0
- data/test/matchers/ensure_length_of_matcher_test.rb +158 -0
- data/test/matchers/have_db_column_matcher_test.rb +169 -0
- data/test/matchers/have_index_matcher_test.rb +74 -0
- data/test/matchers/have_named_scope_matcher_test.rb +65 -0
- data/test/matchers/have_readonly_attributes_matcher_test.rb +29 -0
- data/test/matchers/validate_acceptance_of_matcher_test.rb +44 -0
- data/test/matchers/validate_numericality_of_matcher_test.rb +52 -0
- data/test/matchers/validate_presence_of_matcher_test.rb +86 -0
- data/test/matchers/validate_uniqueness_of_matcher_test.rb +141 -0
- data/test/model_builder.rb +61 -0
- data/test/other/autoload_macro_test.rb +18 -0
- data/test/other/helpers_test.rb +58 -0
- data/test/rails_root/config/database.yml +1 -1
- data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
- data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
- data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
- data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
- data/test/test_helper.rb +3 -1
- data/test/unit/address_test.rb +1 -1
- data/test/unit/dog_test.rb +1 -1
- data/test/unit/post_test.rb +4 -4
- data/test/unit/product_test.rb +2 -2
- data/test/unit/tag_test.rb +2 -1
- data/test/unit/user_test.rb +8 -7
- metadata +49 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class HaveIndexMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "have_index" do
|
|
6
|
+
setup do
|
|
7
|
+
@matcher = have_index(:age)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
should "accept an existing index" do
|
|
11
|
+
db_connection = create_table 'superheros' do |table|
|
|
12
|
+
table.integer :age
|
|
13
|
+
end
|
|
14
|
+
db_connection.add_index :superheros, :age
|
|
15
|
+
define_model_class 'Superhero'
|
|
16
|
+
assert_accepts @matcher, Superhero.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
should "reject a nonexistent index" do
|
|
20
|
+
define_model :superhero
|
|
21
|
+
assert_rejects @matcher, Superhero.new
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context "have_index with unique option" do
|
|
26
|
+
setup do
|
|
27
|
+
@matcher = have_index(:ssn).unique(true)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
should "accept an index of correct unique" do
|
|
31
|
+
db_connection = create_table 'superheros' do |table|
|
|
32
|
+
table.integer :ssn
|
|
33
|
+
end
|
|
34
|
+
db_connection.add_index :superheros, :ssn, :unique => true
|
|
35
|
+
define_model_class 'Superhero'
|
|
36
|
+
assert_accepts @matcher, Superhero.new
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
should "reject an index of wrong unique" do
|
|
40
|
+
db_connection = create_table 'superheros' do |table|
|
|
41
|
+
table.integer :ssn
|
|
42
|
+
end
|
|
43
|
+
db_connection.add_index :superheros, :ssn, :unique => false
|
|
44
|
+
define_model_class 'Superhero'
|
|
45
|
+
assert_rejects @matcher, Superhero.new
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
context "have_index on multiple columns" do
|
|
50
|
+
setup do
|
|
51
|
+
@matcher = have_index([:geocodable_type, :geocodable_id])
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
should "accept an existing index" do
|
|
55
|
+
db_connection = create_table 'geocodings' do |table|
|
|
56
|
+
table.integer :geocodable_id
|
|
57
|
+
table.string :geocodable_type
|
|
58
|
+
end
|
|
59
|
+
db_connection.add_index :geocodings, [:geocodable_type, :geocodable_id]
|
|
60
|
+
define_model_class 'Geocoding'
|
|
61
|
+
assert_accepts @matcher, Geocoding.new
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
should "reject a nonexistant index" do
|
|
65
|
+
db_connection = create_table 'geocodings' do |table|
|
|
66
|
+
table.integer :geocodable_id
|
|
67
|
+
table.string :geocodable_type
|
|
68
|
+
end
|
|
69
|
+
define_model_class 'Geocoding'
|
|
70
|
+
assert_rejects @matcher, Geocoding.new
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class HaveNamedScopeMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "an attribute with a named scope" do
|
|
6
|
+
setup do
|
|
7
|
+
define_model :example, :attr => :string do
|
|
8
|
+
named_scope :xyz, lambda {|n|
|
|
9
|
+
{ :order => :attr }
|
|
10
|
+
}
|
|
11
|
+
end
|
|
12
|
+
@model = Example.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
should "accept having a scope with the correct signature" do
|
|
16
|
+
assert_accepts have_named_scope("xyz(1)"), @model
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
should "accept having a scope with the correct signature and find options" do
|
|
20
|
+
assert_accepts have_named_scope("xyz(1)").finding(:order => :attr), @model
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
should "reject having a scope with incorrect find options" do
|
|
24
|
+
assert_rejects have_named_scope("xyz(1)").
|
|
25
|
+
finding(:order => 'attr DESC'),
|
|
26
|
+
@model
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
should "reject having a scope with another name" do
|
|
30
|
+
assert_rejects have_named_scope("abc(1)"), @model
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
should "evaluate the scope in the correct context" do
|
|
36
|
+
define_model :example, :attr => :string do
|
|
37
|
+
named_scope :xyz, lambda {|n|
|
|
38
|
+
{ :order => n }
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
model = Example.new
|
|
42
|
+
@order = :attr
|
|
43
|
+
assert_accepts have_named_scope("xyz(@order)").
|
|
44
|
+
finding(:order => @order).
|
|
45
|
+
in_context(self),
|
|
46
|
+
model
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
context "a method that does not return a scope" do
|
|
50
|
+
setup do
|
|
51
|
+
klass = Class.new
|
|
52
|
+
klass.class_eval do
|
|
53
|
+
def self.xyz
|
|
54
|
+
'xyz'
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
@model = klass.new
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
should "reject having a named scope with that name" do
|
|
61
|
+
assert_rejects have_named_scope(:xyz), @model
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class HaveReadonlyAttributesMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "an attribute that cannot be set after being saved" do
|
|
6
|
+
setup do
|
|
7
|
+
define_model :example, :attr => :string do
|
|
8
|
+
attr_readonly :attr
|
|
9
|
+
end
|
|
10
|
+
@model = Example.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
should "accept being read-only" do
|
|
14
|
+
assert_accepts have_readonly_attribute(:attr), @model
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
context "an attribute that can be set after being saved" do
|
|
19
|
+
setup do
|
|
20
|
+
define_model :example, :attr => :string
|
|
21
|
+
@model = Example.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
should "accept being read-only" do
|
|
25
|
+
assert_rejects have_readonly_attribute(:attr), @model
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class ValidateAcceptanceOfMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "an attribute which must be accepted" do
|
|
6
|
+
setup do
|
|
7
|
+
@model = define_model(:example) do
|
|
8
|
+
validates_acceptance_of :attr
|
|
9
|
+
end.new
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
should "require that attribute to be accepted" do
|
|
13
|
+
assert_accepts validate_acceptance_of(:attr), @model
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
should "not overwrite the default message with nil" do
|
|
17
|
+
assert_accepts validate_acceptance_of(:attr).with_message(nil), @model
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
context "an attribute that does not need to be accepted" do
|
|
22
|
+
setup do
|
|
23
|
+
@model = define_model(:example, :attr => :string).new
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
should "not require that attribute to be accepted" do
|
|
27
|
+
assert_rejects validate_acceptance_of(:attr), @model
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
context "an attribute which must be accepted with a custom message" do
|
|
32
|
+
setup do
|
|
33
|
+
@model = define_model(:example) do
|
|
34
|
+
validates_acceptance_of :attr, :message => 'custom'
|
|
35
|
+
end.new
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
should "require that attribute to be accepted with that message" do
|
|
39
|
+
assert_accepts validate_acceptance_of(:attr).with_message(/custom/),
|
|
40
|
+
@model
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class ValidateNumericalityOfMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "a numeric attribute" do
|
|
6
|
+
setup do
|
|
7
|
+
define_model :example, :attr => :string do
|
|
8
|
+
validates_numericality_of :attr
|
|
9
|
+
end
|
|
10
|
+
@model = Example.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
should "only allow numeric values for that attribute" do
|
|
14
|
+
assert_accepts validate_numericality_of(:attr), @model
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
should "not override the default message with a blank" do
|
|
18
|
+
assert_accepts validate_numericality_of(:attr).with_message(nil),
|
|
19
|
+
@model
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context "a numeric attribute with a custom validation message" do
|
|
24
|
+
setup do
|
|
25
|
+
define_model :example, :attr => :string do
|
|
26
|
+
validates_numericality_of :attr, :message => 'custom'
|
|
27
|
+
end
|
|
28
|
+
@model = Example.new
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
should "only allow numeric values for that attribute with that message" do
|
|
32
|
+
assert_accepts validate_numericality_of(:attr).
|
|
33
|
+
with_message(/custom/),
|
|
34
|
+
@model
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
should "not allow numeric values for that attribute with another message" do
|
|
38
|
+
assert_rejects validate_numericality_of(:attr), @model
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context "a non-numeric attribute" do
|
|
43
|
+
setup do
|
|
44
|
+
@model = define_model(:example, :attr => :string).new
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
should "not only allow numeric values for that attribute" do
|
|
48
|
+
assert_rejects validate_numericality_of(:attr), @model
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class ValidatePresenceOfMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "a required attribute" do
|
|
6
|
+
setup do
|
|
7
|
+
define_model :example, :attr => :string do
|
|
8
|
+
validates_presence_of :attr
|
|
9
|
+
end
|
|
10
|
+
@model = Example.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
should "require a value" do
|
|
14
|
+
assert_accepts validate_presence_of(:attr), @model
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
should "not override the default message with a blank" do
|
|
18
|
+
assert_accepts validate_presence_of(:attr).with_message(nil), @model
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
context "an optional attribute" do
|
|
23
|
+
setup do
|
|
24
|
+
@model = define_model(:example, :attr => :string).new
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
should "not require a value" do
|
|
28
|
+
assert_rejects validate_presence_of(:attr), @model
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context "a required has_many association" do
|
|
33
|
+
setup do
|
|
34
|
+
define_model :child
|
|
35
|
+
@model = define_model :parent do
|
|
36
|
+
has_many :children
|
|
37
|
+
validates_presence_of :children
|
|
38
|
+
end.new
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
should "require the attribute to be set" do
|
|
42
|
+
assert_accepts validate_presence_of(:children), @model
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context "an optional has_many association" do
|
|
47
|
+
setup do
|
|
48
|
+
define_model :child
|
|
49
|
+
@model = define_model :parent do
|
|
50
|
+
has_many :children
|
|
51
|
+
end.new
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
should "not require the attribute to be set" do
|
|
55
|
+
assert_rejects validate_presence_of(:children), @model
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context "a required has_and_belongs_to_many association" do
|
|
60
|
+
setup do
|
|
61
|
+
define_model :child
|
|
62
|
+
@model = define_model :parent do
|
|
63
|
+
has_and_belongs_to_many :children
|
|
64
|
+
validates_presence_of :children
|
|
65
|
+
end.new
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
should "require the attribute to be set" do
|
|
69
|
+
assert_accepts validate_presence_of(:children), @model
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
context "an optional has_and_belongs_to_many association" do
|
|
74
|
+
setup do
|
|
75
|
+
define_model :child
|
|
76
|
+
@model = define_model :parent do
|
|
77
|
+
has_and_belongs_to_many :children
|
|
78
|
+
end.new
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
should "not require the attribute to be set" do
|
|
82
|
+
assert_rejects validate_presence_of(:children), @model
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
end
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
|
|
5
|
+
context "a unique attribute" do
|
|
6
|
+
setup do
|
|
7
|
+
@model = define_model(:example, :attr => :string,
|
|
8
|
+
:other => :integer) do
|
|
9
|
+
validates_uniqueness_of :attr
|
|
10
|
+
end.new
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context "with an existing value" do
|
|
14
|
+
setup do
|
|
15
|
+
@existing = Example.create!(:attr => 'value', :other => 1)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
should "require a unique value for that attribute" do
|
|
19
|
+
assert_accepts validate_uniqueness_of(:attr), @model
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
should "pass when the subject is an existing record" do
|
|
23
|
+
assert_accepts validate_uniqueness_of(:attr), @existing
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
should "fail when a scope is specified" do
|
|
27
|
+
assert_rejects validate_uniqueness_of(:attr).scoped_to(:other), @model
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
context "without an existing value" do
|
|
32
|
+
setup do
|
|
33
|
+
assert_nil Example.find(:first)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
should "fail to require a unique value" do
|
|
37
|
+
assert_rejects validate_uniqueness_of(:attr), @model
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
context "a unique attribute with a custom error and an existing value" do
|
|
43
|
+
setup do
|
|
44
|
+
@model = define_model(:example, :attr => :string) do
|
|
45
|
+
validates_uniqueness_of :attr, :message => 'Bad value'
|
|
46
|
+
end.new
|
|
47
|
+
Example.create!
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
should "fail when checking the default message" do
|
|
51
|
+
assert_rejects validate_uniqueness_of(:attr), @model
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
should "fail when checking a message that doesn't match" do
|
|
55
|
+
assert_rejects validate_uniqueness_of(:attr).with_message(/abc/i), @model
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
should "pass when checking a message that matches" do
|
|
59
|
+
assert_accepts validate_uniqueness_of(:attr).with_message(/bad/i), @model
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "a scoped unique attribute with an existing value" do
|
|
64
|
+
setup do
|
|
65
|
+
@model = define_model(:example, :attr => :string,
|
|
66
|
+
:scope1 => :integer,
|
|
67
|
+
:scope2 => :integer) do
|
|
68
|
+
validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
|
|
69
|
+
end.new
|
|
70
|
+
@existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
should "pass when the correct scope is specified" do
|
|
74
|
+
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
|
|
75
|
+
@model
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
should "pass when the subject is an existing record" do
|
|
79
|
+
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
|
|
80
|
+
@existing
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
should "fail when a different scope is specified" do
|
|
84
|
+
assert_rejects validate_uniqueness_of(:attr).scoped_to(:scope1), @model
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
should "fail when no scope is specified" do
|
|
88
|
+
assert_rejects validate_uniqueness_of(:attr), @model
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
should "fail when a non-existent attribute is specified as a scope" do
|
|
92
|
+
assert_rejects validate_uniqueness_of(:attr).scoped_to(:fake), @model
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
context "a non-unique attribute with an existing value" do
|
|
97
|
+
setup do
|
|
98
|
+
@model = define_model(:example, :attr => :string).new
|
|
99
|
+
Example.create!(:attr => 'value')
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
should "not require a unique value for that attribute" do
|
|
103
|
+
assert_rejects validate_uniqueness_of(:attr), @model
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
context "a case sensitive unique attribute with an existing value" do
|
|
108
|
+
setup do
|
|
109
|
+
@model = define_model(:example, :attr => :string) do
|
|
110
|
+
validates_uniqueness_of :attr, :case_sensitive => true
|
|
111
|
+
end.new
|
|
112
|
+
Example.create!(:attr => 'value')
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
should "not require a unique, case-insensitive value for that attribute" do
|
|
116
|
+
assert_rejects validate_uniqueness_of(:attr).case_insensitive, @model
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
should "require a unique, case-sensitive value for that attribute" do
|
|
120
|
+
assert_accepts validate_uniqueness_of(:attr), @model
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
context "a case sensitive unique integer attribute with an existing value" do
|
|
125
|
+
setup do
|
|
126
|
+
@model = define_model(:example, :attr => :integer) do
|
|
127
|
+
validates_uniqueness_of :attr, :case_sensitive => true
|
|
128
|
+
end.new
|
|
129
|
+
Example.create!(:attr => 'value')
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
should "require a unique, case-insensitive value for that attribute" do
|
|
133
|
+
assert_accepts validate_uniqueness_of(:attr).case_insensitive, @model
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
should "require a unique, case-sensitive value for that attribute" do
|
|
137
|
+
assert_accepts validate_uniqueness_of(:attr), @model
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
class Test::Unit::TestCase
|
|
2
|
+
def create_table(table_name, &block)
|
|
3
|
+
connection = ActiveRecord::Base.connection
|
|
4
|
+
|
|
5
|
+
begin
|
|
6
|
+
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
7
|
+
connection.create_table(table_name, &block)
|
|
8
|
+
@created_tables ||= []
|
|
9
|
+
@created_tables << table_name
|
|
10
|
+
connection
|
|
11
|
+
rescue Exception => e
|
|
12
|
+
connection.execute("DROP TABLE IF EXISTS #{table_name}")
|
|
13
|
+
raise e
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def define_model_class(class_name, &block)
|
|
18
|
+
klass = Class.new(ActiveRecord::Base)
|
|
19
|
+
Object.const_set(class_name, klass)
|
|
20
|
+
|
|
21
|
+
klass.class_eval(&block) if block_given?
|
|
22
|
+
|
|
23
|
+
@defined_constants ||= []
|
|
24
|
+
@defined_constants << class_name
|
|
25
|
+
|
|
26
|
+
klass
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def define_model(name, columns = {}, &block)
|
|
30
|
+
class_name = name.to_s.pluralize.classify
|
|
31
|
+
table_name = class_name.tableize
|
|
32
|
+
|
|
33
|
+
create_table(table_name) do |table|
|
|
34
|
+
columns.each do |name, type|
|
|
35
|
+
table.column name, type
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
define_model_class(class_name, &block)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def teardown_with_models
|
|
43
|
+
if @defined_constants
|
|
44
|
+
@defined_constants.each do |class_name|
|
|
45
|
+
Object.send(:remove_const, class_name)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
if @created_tables
|
|
50
|
+
@created_tables.each do |table_name|
|
|
51
|
+
ActiveRecord::Base.
|
|
52
|
+
connection.
|
|
53
|
+
execute("DROP TABLE IF EXISTS #{table_name}")
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
teardown_without_models
|
|
58
|
+
end
|
|
59
|
+
alias_method :teardown_without_models, :teardown
|
|
60
|
+
alias_method :teardown, :teardown_with_models
|
|
61
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
|
2
|
+
|
|
3
|
+
class AutoloadMacroTest < Test::Unit::TestCase # :nodoc:
|
|
4
|
+
context "The macro auto-loader" do
|
|
5
|
+
should "load macros from the plugins" do
|
|
6
|
+
assert self.class.respond_to?('plugin_macro')
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
should "load macros from the gems" do
|
|
10
|
+
assert self.class.respond_to?('gem_macro')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
should "load custom macros from ROOT/test/shoulda_macros" do
|
|
14
|
+
assert self.class.respond_to?('custom_macro')
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
data/test/other/helpers_test.rb
CHANGED
|
@@ -180,4 +180,62 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
|
|
|
180
180
|
end
|
|
181
181
|
end
|
|
182
182
|
end
|
|
183
|
+
|
|
184
|
+
context "a matching matcher" do
|
|
185
|
+
setup do
|
|
186
|
+
@matcher = stub('matcher', :matches? => true,
|
|
187
|
+
:failure_message => 'bad failure message',
|
|
188
|
+
:negative_failure_message => 'big time failure')
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
should "pass when given to assert_accepts" do
|
|
192
|
+
assert_accepts @matcher, 'target'
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
context "when given to assert_rejects" do
|
|
196
|
+
setup do
|
|
197
|
+
begin
|
|
198
|
+
assert_rejects @matcher, 'target'
|
|
199
|
+
rescue Test::Unit::AssertionFailedError => @error
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
should "fail" do
|
|
204
|
+
assert_not_nil @error
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
should "use the error message from the matcher" do
|
|
208
|
+
assert_equal 'big time failure', @error.message
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
context "a non-matching matcher" do
|
|
214
|
+
setup do
|
|
215
|
+
@matcher = stub('matcher', :matches? => false,
|
|
216
|
+
:failure_message => 'big time failure',
|
|
217
|
+
:negative_failure_message => 'bad failure message')
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
should "pass when given to assert_rejects" do
|
|
221
|
+
assert_rejects @matcher, 'target'
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
context "when given to assert_accepts" do
|
|
225
|
+
setup do
|
|
226
|
+
begin
|
|
227
|
+
assert_accepts @matcher, 'target'
|
|
228
|
+
rescue Test::Unit::AssertionFailedError => @error
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
should "fail" do
|
|
233
|
+
assert_not_nil @error
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
should "use the error message from the matcher" do
|
|
237
|
+
assert_equal 'big time failure', @error.message
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
183
241
|
end
|
|
File without changes
|
data/test/test_helper.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require 'fileutils'
|
|
2
|
+
|
|
2
3
|
# Load the environment
|
|
3
|
-
ENV['RAILS_ENV'] = '
|
|
4
|
+
ENV['RAILS_ENV'] = 'test'
|
|
4
5
|
|
|
5
6
|
rails_root = File.dirname(__FILE__) + '/rails_root'
|
|
6
7
|
|
|
@@ -31,3 +32,4 @@ class Test::Unit::TestCase #:nodoc:
|
|
|
31
32
|
end
|
|
32
33
|
|
|
33
34
|
require 'test/fail_macros'
|
|
35
|
+
require 'test/model_builder'
|
data/test/unit/address_test.rb
CHANGED
|
@@ -4,7 +4,7 @@ class AddressTest < Test::Unit::TestCase
|
|
|
4
4
|
fixtures :all
|
|
5
5
|
|
|
6
6
|
should_belong_to :addressable
|
|
7
|
-
|
|
7
|
+
should_validate_uniqueness_of :title, :scoped_to => [:addressable_id, :addressable_type]
|
|
8
8
|
should_ensure_length_at_least :zip, 5
|
|
9
9
|
should_only_allow_numeric_values_for :zip
|
|
10
10
|
end
|