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.
- 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
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher do
|
4
|
+
it 'accepts an existing database column' do
|
5
|
+
model(:nickname => :string).should have_db_column(:nickname)
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'rejects a nonexistent database column' do
|
9
|
+
define_model(:employee).new.should_not have_db_column(:nickname)
|
10
|
+
end
|
11
|
+
|
12
|
+
context '#of_type' do
|
13
|
+
it 'accepts a column of correct type' do
|
14
|
+
model(:nickname => :string).
|
15
|
+
should have_db_column(:nickname).of_type(:string)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'rejects a nonexistent database column' do
|
19
|
+
define_model(:superhero).new.
|
20
|
+
should_not have_db_column(:nickname).of_type(:string)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'rejects a column of wrong type' do
|
24
|
+
model(:nickname => :integer).
|
25
|
+
should_not have_db_column(:nickname).of_type(:string)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'with precision option' do
|
30
|
+
it 'accepts a column of correct precision' do
|
31
|
+
with_table(:salary, :decimal, :precision => 5).
|
32
|
+
should have_db_column(:salary).with_options(:precision => 5)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'rejects a column of wrong precision' do
|
36
|
+
with_table(:salary, :decimal, :precision => 6).
|
37
|
+
should_not have_db_column(:salary).with_options(:precision => 5)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with limit option' do
|
42
|
+
it 'accepts a column of correct limit' do
|
43
|
+
with_table(:email, :string, :limit => 255).
|
44
|
+
should have_db_column(:email).with_options(:limit => 255)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'rejects a column of wrong limit' do
|
48
|
+
with_table(:email, :string, :limit => 100).
|
49
|
+
should_not have_db_column(:email).with_options(:limit => 255)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'with default option' do
|
54
|
+
it 'accepts a column with correct default' do
|
55
|
+
with_table(:admin, :boolean, :default => false).
|
56
|
+
should have_db_column(:admin).with_options(:default => false)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'rejects a column with wrong default' do
|
60
|
+
with_table(:admin, :boolean, :default => true).
|
61
|
+
should_not have_db_column(:admin).with_options(:default => false)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'with null option' do
|
66
|
+
it 'accepts a column of correct null' do
|
67
|
+
with_table(:admin, :boolean, :null => false).
|
68
|
+
should have_db_column(:admin).with_options(:null => false)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'rejects a column of wrong null' do
|
72
|
+
with_table(:admin, :boolean, :null => true).
|
73
|
+
should_not have_db_column(:admin).with_options(:null => false)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context 'with scale option' do
|
78
|
+
it 'accepts a column of correct scale' do
|
79
|
+
with_table(:salary, :decimal, :precision => 10, :scale => 2).
|
80
|
+
should have_db_column(:salary).with_options(:scale => 2)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'rejects a column of wrong scale' do
|
84
|
+
with_table(:salary, :decimal, :precision => 10, :scale => 4).
|
85
|
+
should_not have_db_column(:salary).with_options(:scale => 2)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'with primary option' do
|
90
|
+
it 'accepts a column that is primary' do
|
91
|
+
with_table(:id, :integer, :primary => true).
|
92
|
+
should have_db_column(:id).with_options(:primary => true)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'rejects a column that is not primary' do
|
96
|
+
with_table(:whatever, :integer, :primary => false).
|
97
|
+
should_not have_db_column(:whatever).with_options(:primary => true)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def model(options = {})
|
102
|
+
define_model(:employee, options).new
|
103
|
+
end
|
104
|
+
|
105
|
+
def with_table(column_name, column_type, options)
|
106
|
+
create_table 'employees' do |table|
|
107
|
+
table.send(column_type, column_name, options)
|
108
|
+
end
|
109
|
+
define_model_class('Employee').new
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher do
|
4
|
+
context 'have_db_index' do
|
5
|
+
it 'accepts an existing index' do
|
6
|
+
with_index_on(:age).should have_db_index(:age)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'rejects a nonexistent index' do
|
10
|
+
define_model(:employee).new.should_not have_db_index(:age)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'have_db_index with unique option' do
|
15
|
+
it 'accepts an index of correct unique' do
|
16
|
+
with_index_on(:ssn, :unique => true).
|
17
|
+
should have_db_index(:ssn).unique(true)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'rejects an index of wrong unique' do
|
21
|
+
with_index_on(:ssn, :unique => false).
|
22
|
+
should_not have_db_index(:ssn).unique(true)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'have_db_index on multiple columns' do
|
27
|
+
it 'accepts an existing index' do
|
28
|
+
db_connection = create_table 'geocodings' do |table|
|
29
|
+
table.integer :geocodable_id
|
30
|
+
table.string :geocodable_type
|
31
|
+
end
|
32
|
+
db_connection.add_index :geocodings, [:geocodable_type, :geocodable_id]
|
33
|
+
define_model_class('Geocoding').new.
|
34
|
+
should have_db_index([:geocodable_type, :geocodable_id])
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'rejects a nonexistent index' do
|
38
|
+
create_table 'geocodings' do |table|
|
39
|
+
table.integer :geocodable_id
|
40
|
+
table.string :geocodable_type
|
41
|
+
end
|
42
|
+
define_model_class('Geocoding').new.
|
43
|
+
should_not have_db_index([:geocodable_type, :geocodable_id])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'join columns with and when describing multiple columns' do
|
48
|
+
have_db_index([:user_id, :post_id]).description.should =~ /on columns user_id and post_id/
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'describes a unique index as unique' do
|
52
|
+
have_db_index(:user_id).unique(true).description.should =~ /a unique index/
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'describes a non-unique index as non-unique' do
|
56
|
+
have_db_index(:user_id).unique(false).description.should =~ /a non-unique index/
|
57
|
+
end
|
58
|
+
|
59
|
+
it "does not display an index's uniqueness when it's not important" do
|
60
|
+
have_db_index(:user_id).description.should_not =~ /unique/
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'allows an IndexDefinition to have a truthy value for unique' do
|
64
|
+
index_definition = stub('ActiveRecord::ConnectionAdapters::IndexDefinition',
|
65
|
+
:unique => 7, :name => :age)
|
66
|
+
matcher = have_db_index(:age).unique(true)
|
67
|
+
matcher.stubs(:matched_index => index_definition)
|
68
|
+
|
69
|
+
with_index_on(:age).should matcher
|
70
|
+
end
|
71
|
+
|
72
|
+
def with_index_on(column_name, index_options = {})
|
73
|
+
create_table 'employees' do |table|
|
74
|
+
table.integer column_name
|
75
|
+
end.add_index(:employees, column_name, index_options)
|
76
|
+
define_model_class('Employee').new
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveRecord::HaveReadonlyAttributeMatcher do
|
4
|
+
context 'a read-only attribute' do
|
5
|
+
it 'accepts' do
|
6
|
+
with_readonly_attr.should have_readonly_attribute(:attr)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'an attribute that is not part of the read-only set' do
|
11
|
+
it 'rejects being read-only' do
|
12
|
+
model = define_model :example, :attr => :string, :other => :string do
|
13
|
+
attr_readonly :attr
|
14
|
+
end.new
|
15
|
+
|
16
|
+
model.should_not have_readonly_attribute(:other)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'an attribute on a class with no readonly attributes' do
|
21
|
+
it 'rejects being read-only' do
|
22
|
+
define_model(:example, :attr => :string).new.
|
23
|
+
should_not have_readonly_attribute(:attr)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'assigns a failure message' do
|
27
|
+
model = define_model(:example, :attr => :string).new
|
28
|
+
matcher = have_readonly_attribute(:attr)
|
29
|
+
|
30
|
+
matcher.matches?(model)
|
31
|
+
|
32
|
+
matcher.failure_message_for_should.should_not be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def with_readonly_attr
|
37
|
+
define_model :example, :attr => :string do
|
38
|
+
attr_readonly :attr
|
39
|
+
end.new
|
40
|
+
end
|
41
|
+
end
|
data/spec/shoulda/{active_record → matchers/active_record}/query_the_database_matcher_spec.rb
RENAMED
@@ -10,36 +10,36 @@ describe Shoulda::Matchers::ActiveRecord::QueryTheDatabaseMatcher do
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
-
it
|
13
|
+
it 'accepts the correct number of queries when there is a single query' do
|
14
14
|
@parent.should query_the_database(1.times).when_calling(:count)
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
17
|
+
it 'accepts any number of queries when no number is specified' do
|
18
18
|
@parent.should query_the_database.when_calling(:count)
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it 'rejects any number of queries when no number is specified' do
|
22
22
|
@parent.should_not query_the_database.when_calling(:to_s)
|
23
23
|
end
|
24
24
|
|
25
|
-
it
|
25
|
+
it 'accepts the correct number of queries when there are two queries' do
|
26
26
|
nonsense = lambda do
|
27
27
|
@parent.create.kittens.create
|
28
28
|
end
|
29
29
|
nonsense.should query_the_database(2.times).when_calling(:call)
|
30
30
|
end
|
31
31
|
|
32
|
-
it
|
32
|
+
it 'rejects the wrong number of queries' do
|
33
33
|
@parent.should_not query_the_database(10.times).when_calling(:count)
|
34
34
|
end
|
35
35
|
|
36
|
-
it
|
36
|
+
it 'accepts fewer than the specified maximum' do
|
37
37
|
@parent.should query_the_database(5.times).or_less.when_calling(:count)
|
38
38
|
end
|
39
39
|
|
40
|
-
it
|
41
|
-
model = stub(
|
42
|
-
model.should_not query_the_database.when_calling(:count).with(
|
43
|
-
model.should have_received(:count).with(
|
40
|
+
it 'passes arguments to the method to examine' do
|
41
|
+
model = stub('Model', :count => nil)
|
42
|
+
model.should_not query_the_database.when_calling(:count).with('arguments')
|
43
|
+
model.should have_received(:count).with('arguments')
|
44
44
|
end
|
45
45
|
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Shoulda::Matchers::ActiveRecord::SerializeMatcher do
|
4
|
+
it 'accepts when the attribute is serialized' do
|
5
|
+
with_serialized_attr.should serialize(:attr)
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'when attribute is not serialized' do
|
9
|
+
it 'rejects' do
|
10
|
+
unserialized_model.should_not serialize(:attr)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'assigns a helpful failure message' do
|
14
|
+
matcher = serialize(:attr)
|
15
|
+
|
16
|
+
matcher.matches?(unserialized_model)
|
17
|
+
|
18
|
+
matcher.failure_message_for_should.should =~ /to serialize the attribute called :attr/
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'assigns a helpful failure message when using #as' do
|
22
|
+
matcher = serialize(:attr).as(Hash)
|
23
|
+
|
24
|
+
matcher.matches?(unserialized_model)
|
25
|
+
|
26
|
+
matcher.failure_message_for_should.should =~ /with a type of Hash/
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'assigns a helpful failure message when using #as_instance_of' do
|
30
|
+
matcher = serialize(:attr).as_instance_of(Hash)
|
31
|
+
|
32
|
+
matcher.matches?(unserialized_model)
|
33
|
+
|
34
|
+
matcher.failure_message_for_should.should =~ /with an instance of Hash/
|
35
|
+
end
|
36
|
+
|
37
|
+
def unserialized_model
|
38
|
+
@model ||= define_model(:example, :attr => :string).new
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'an attribute that is serialized as a specific type' do
|
43
|
+
it 'accepts when the types match' do
|
44
|
+
with_serialized_attr(Hash).should serialize(:attr).as(Hash)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'rejects when the types do not match' do
|
48
|
+
with_serialized_attr(Hash).should_not serialize(:attr).as(String)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'rejects when using as_instance_of' do
|
52
|
+
with_serialized_attr(Hash).should_not serialize(:attr).as_instance_of(Hash)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'a serializer that is an instance of a class' do
|
57
|
+
it 'accepts when using #as_instance_of' do
|
58
|
+
define_serializer(:ExampleSerializer)
|
59
|
+
with_serialized_attr(ExampleSerializer.new).
|
60
|
+
should serialize(:attr).as_instance_of(ExampleSerializer)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'rejects when using #as' do
|
64
|
+
define_serializer(:ExampleSerializer)
|
65
|
+
with_serialized_attr(ExampleSerializer.new).
|
66
|
+
should_not serialize(:attr).as(ExampleSerializer)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def with_serialized_attr(type = nil)
|
71
|
+
define_model(:example, :attr => :string) do
|
72
|
+
if type
|
73
|
+
serialize :attr, type
|
74
|
+
else
|
75
|
+
serialize :attr
|
76
|
+
end
|
77
|
+
end.new
|
78
|
+
end
|
79
|
+
|
80
|
+
def define_serializer(name)
|
81
|
+
define_class(name) do
|
82
|
+
def load(*); end
|
83
|
+
def dump(*); end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -1,33 +1,59 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Shoulda::Matchers::Independent::DelegateMatcher do
|
4
|
+
context '#description' do
|
5
|
+
context 'by default' do
|
6
|
+
it 'states that it should delegate method to the right object' do
|
7
|
+
matcher = delegate_method(:method_name).to(:target)
|
8
|
+
|
9
|
+
matcher.description.should == 'delegate method #method_name to :target'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with #as chain' do
|
14
|
+
it 'states that it should delegate method to the right object and method' do
|
15
|
+
matcher = delegate_method(:method_name).to(:target).as(:alternate)
|
16
|
+
|
17
|
+
matcher.description.should == 'delegate method #method_name to :target as #alternate'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with #with_argument chain' do
|
22
|
+
it 'states that it should delegate method to the right object with right argument' do
|
23
|
+
matcher = delegate_method(:method_name).to(:target).with_arguments(:foo, :bar => [1, 2])
|
24
|
+
|
25
|
+
matcher.description.should ==
|
26
|
+
'delegate method #method_name to :target with arguments: [:foo, {:bar=>[1, 2]}]'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
4
31
|
it 'supports chaining on #to' do
|
5
32
|
matcher = delegate_method(:method)
|
33
|
+
|
6
34
|
matcher.to(:another_method).should == matcher
|
7
35
|
end
|
8
36
|
|
9
37
|
it 'supports chaining on #with_arguments' do
|
10
38
|
matcher = delegate_method(:method)
|
39
|
+
|
11
40
|
matcher.with_arguments(1, 2, 3).should == matcher
|
12
41
|
end
|
13
42
|
|
14
43
|
it 'supports chaining on #as' do
|
15
44
|
matcher = delegate_method(:method)
|
45
|
+
|
16
46
|
matcher.as(:some_other_method).should == matcher
|
17
47
|
end
|
18
48
|
|
19
|
-
it '
|
20
|
-
|
21
|
-
|
22
|
-
object.should delegate_method(:name)
|
23
|
-
}.to raise_exception Shoulda::Matchers::Independent::DelegateMatcher::TargetNotDefinedError
|
49
|
+
it 'raises an error if no delegation target is defined' do
|
50
|
+
expect { Object.new.should delegate_method(:name) }.
|
51
|
+
to raise_exception described_class::TargetNotDefinedError
|
24
52
|
end
|
25
53
|
|
26
|
-
it '
|
27
|
-
|
28
|
-
|
29
|
-
object.should_not delegate_method(:name).to(:anyone)
|
30
|
-
}.to raise_exception Shoulda::Matchers::Independent::DelegateMatcher::InvalidDelegateMatcher
|
54
|
+
it 'raises an error if called with #should_not' do
|
55
|
+
expect { Object.new.should_not delegate_method(:name).to(:anyone) }.
|
56
|
+
to raise_exception described_class::InvalidDelegateMatcher
|
31
57
|
end
|
32
58
|
|
33
59
|
context 'given a method that does not delegate' do
|
@@ -39,9 +65,10 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
39
65
|
end
|
40
66
|
end
|
41
67
|
|
42
|
-
it '
|
68
|
+
it 'rejects' do
|
43
69
|
post_office = PostOffice.new
|
44
70
|
matcher = delegate_method(:deliver_mail).to(:mailman)
|
71
|
+
|
45
72
|
matcher.matches?(post_office).should be_false
|
46
73
|
end
|
47
74
|
|
@@ -52,7 +79,7 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
52
79
|
matcher.matches?(post_office)
|
53
80
|
|
54
81
|
message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman'
|
55
|
-
matcher.
|
82
|
+
matcher.failure_message_for_should.should == message
|
56
83
|
end
|
57
84
|
|
58
85
|
it 'uses the proper syntax for class methods in errors' do
|
@@ -61,12 +88,12 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
61
88
|
matcher.matches?(PostOffice)
|
62
89
|
|
63
90
|
message = 'Expected PostOffice.deliver_mail to delegate to PostOffice.mailman'
|
64
|
-
matcher.
|
91
|
+
matcher.failure_message_for_should.should == message
|
65
92
|
end
|
66
93
|
end
|
67
94
|
|
68
95
|
context 'given a method that delegates properly' do
|
69
|
-
|
96
|
+
it 'accepts' do
|
70
97
|
define_class(:mailman)
|
71
98
|
define_class(:post_office) do
|
72
99
|
def deliver_mail
|
@@ -76,12 +103,7 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
76
103
|
def mailman
|
77
104
|
Mailman.new
|
78
105
|
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'matches' do
|
83
|
-
post_office = PostOffice.new
|
84
|
-
post_office.should delegate_method(:deliver_mail).to(:mailman)
|
106
|
+
end.new.should delegate_method(:deliver_mail).to(:mailman)
|
85
107
|
end
|
86
108
|
end
|
87
109
|
|
@@ -100,17 +122,17 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
100
122
|
end
|
101
123
|
|
102
124
|
context 'when given the correct arguments' do
|
103
|
-
it '
|
104
|
-
|
105
|
-
|
106
|
-
post_office.should matcher
|
125
|
+
it 'accepts' do
|
126
|
+
PostOffice.new.should delegate_method(:deliver_mail).
|
127
|
+
to(:mailman).with_arguments('221B Baker St.', :hastily => true)
|
107
128
|
end
|
108
129
|
end
|
109
130
|
|
110
131
|
context 'when not given the correct arguments' do
|
111
|
-
it '
|
132
|
+
it 'rejects' do
|
112
133
|
post_office = PostOffice.new
|
113
|
-
matcher = delegate_method(:deliver_mail).to(:mailman).
|
134
|
+
matcher = delegate_method(:deliver_mail).to(:mailman).
|
135
|
+
with_arguments('123 Nowhere Ln.')
|
114
136
|
matcher.matches?(post_office).should be_false
|
115
137
|
end
|
116
138
|
|
@@ -121,7 +143,7 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
121
143
|
matcher.matches?(post_office)
|
122
144
|
|
123
145
|
message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman with arguments: ["123 Nowhere Ln."]'
|
124
|
-
matcher.
|
146
|
+
matcher.failure_message_for_should.should == message
|
125
147
|
end
|
126
148
|
end
|
127
149
|
end
|
@@ -141,15 +163,14 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
141
163
|
end
|
142
164
|
|
143
165
|
context 'when given the correct method name' do
|
144
|
-
it '
|
145
|
-
|
146
|
-
|
147
|
-
post_office.should matcher
|
166
|
+
it 'accepts' do
|
167
|
+
PostOffice.new.
|
168
|
+
should delegate_method(:deliver_mail).to(:mailman).as(:deliver_mail_and_avoid_dogs)
|
148
169
|
end
|
149
170
|
end
|
150
171
|
|
151
172
|
context 'when given an incorrect method name' do
|
152
|
-
it '
|
173
|
+
it 'rejects' do
|
153
174
|
post_office = PostOffice.new
|
154
175
|
matcher = delegate_method(:deliver_mail).to(:mailman).as(:watch_tv)
|
155
176
|
matcher.matches?(post_office).should be_false
|
@@ -161,8 +182,8 @@ describe Shoulda::Matchers::Independent::DelegateMatcher do
|
|
161
182
|
|
162
183
|
matcher.matches?(post_office)
|
163
184
|
|
164
|
-
message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman as
|
165
|
-
matcher.
|
185
|
+
message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman as #watch_tv'
|
186
|
+
matcher.failure_message_for_should.should == message
|
166
187
|
end
|
167
188
|
end
|
168
189
|
end
|