shoulda-matchers 4.0.0.rc1 → 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +72 -40
- data/lib/shoulda/matchers/action_controller.rb +2 -0
- data/lib/shoulda/matchers/active_model.rb +2 -3
- data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +1 -0
- data/lib/shoulda/matchers/active_record.rb +2 -0
- data/lib/shoulda/matchers/active_record/association_matcher.rb +34 -0
- data/lib/shoulda/matchers/active_record/association_matchers/optional_matcher.rb +27 -4
- data/lib/shoulda/matchers/active_record/association_matchers/required_matcher.rb +27 -4
- data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +1 -1
- data/lib/shoulda/matchers/independent.rb +2 -1
- data/lib/shoulda/matchers/rails_shim.rb +5 -9
- data/lib/shoulda/matchers/version.rb +1 -1
- data/lib/shoulda/matchers/warn.rb +1 -0
- data/shoulda-matchers.gemspec +11 -3
- metadata +13 -340
- data/.gitignore +0 -12
- data/.hound.yml +0 -3
- data/.hound/ruby.yml +0 -1062
- data/.python-version +0 -1
- data/.rubocop.yml +0 -15
- data/.travis.yml +0 -21
- data/.yardopts +0 -10
- data/Appraisals +0 -105
- data/CONTRIBUTING.md +0 -172
- data/Gemfile +0 -15
- data/Gemfile.lock +0 -59
- data/MAINTAINING.md +0 -250
- data/NEWS.md +0 -1235
- data/Rakefile +0 -46
- data/bin/setup +0 -190
- data/custom_plan.rb +0 -104
- data/doc_config/gh-pages/index.html.erb +0 -9
- data/doc_config/yard/setup.rb +0 -22
- data/doc_config/yard/templates/default/fulldoc/html/css/bootstrap.css +0 -5967
- data/doc_config/yard/templates/default/fulldoc/html/css/full_list.css +0 -12
- data/doc_config/yard/templates/default/fulldoc/html/css/global.css +0 -66
- data/doc_config/yard/templates/default/fulldoc/html/css/solarized.css +0 -69
- data/doc_config/yard/templates/default/fulldoc/html/css/style.css +0 -312
- data/doc_config/yard/templates/default/fulldoc/html/full_list.erb +0 -26
- data/doc_config/yard/templates/default/fulldoc/html/full_list_class.erb +0 -1
- data/doc_config/yard/templates/default/fulldoc/html/full_list_method.erb +0 -8
- data/doc_config/yard/templates/default/fulldoc/html/js/app.js +0 -281
- data/doc_config/yard/templates/default/fulldoc/html/js/full_list.js +0 -1
- data/doc_config/yard/templates/default/fulldoc/html/js/jquery.stickyheaders.js +0 -289
- data/doc_config/yard/templates/default/fulldoc/html/js/underscore.min.js +0 -6
- data/doc_config/yard/templates/default/fulldoc/html/setup.rb +0 -35
- data/doc_config/yard/templates/default/layout/html/breadcrumb.erb +0 -14
- data/doc_config/yard/templates/default/layout/html/fonts.erb +0 -1
- data/doc_config/yard/templates/default/layout/html/footer.erb +0 -6
- data/doc_config/yard/templates/default/layout/html/layout.erb +0 -23
- data/doc_config/yard/templates/default/layout/html/search.erb +0 -13
- data/doc_config/yard/templates/default/layout/html/setup.rb +0 -40
- data/doc_config/yard/templates/default/method_details/html/source.erb +0 -10
- data/doc_config/yard/templates/default/module/html/box_info.erb +0 -31
- data/gemfiles/4.2.gemfile +0 -39
- data/gemfiles/4.2.gemfile.lock +0 -246
- data/gemfiles/5.0.gemfile +0 -37
- data/gemfiles/5.0.gemfile.lock +0 -238
- data/gemfiles/5.1.gemfile +0 -38
- data/gemfiles/5.1.gemfile.lock +0 -254
- data/gemfiles/5.2.gemfile +0 -40
- data/gemfiles/5.2.gemfile.lock +0 -273
- data/script/install_gems_in_all_appraisals +0 -16
- data/script/run_all_tests +0 -16
- data/script/supported_ruby_versions +0 -7
- data/script/update_gem_in_all_appraisals +0 -17
- data/script/update_gems_in_all_appraisals +0 -16
- data/spec/acceptance/active_model_integration_spec.rb +0 -23
- data/spec/acceptance/independent_matchers_spec.rb +0 -125
- data/spec/acceptance/multiple_libraries_integration_spec.rb +0 -55
- data/spec/acceptance/rails_integration_spec.rb +0 -156
- data/spec/acceptance_spec_helper.rb +0 -23
- data/spec/doublespeak_spec_helper.rb +0 -2
- data/spec/report_warnings.rb +0 -7
- data/spec/spec_helper.rb +0 -20
- data/spec/support/acceptance/adds_shoulda_matchers_to_project.rb +0 -133
- data/spec/support/acceptance/helpers.rb +0 -33
- data/spec/support/acceptance/helpers/active_model_helpers.rb +0 -11
- data/spec/support/acceptance/helpers/array_helpers.rb +0 -13
- data/spec/support/acceptance/helpers/base_helpers.rb +0 -19
- data/spec/support/acceptance/helpers/command_helpers.rb +0 -68
- data/spec/support/acceptance/helpers/file_helpers.rb +0 -19
- data/spec/support/acceptance/helpers/gem_helpers.rb +0 -31
- data/spec/support/acceptance/helpers/minitest_helpers.rb +0 -11
- data/spec/support/acceptance/helpers/n_unit_helpers.rb +0 -25
- data/spec/support/acceptance/helpers/pluralization_helpers.rb +0 -13
- data/spec/support/acceptance/helpers/rails_migration_helpers.rb +0 -21
- data/spec/support/acceptance/helpers/rails_version_helpers.rb +0 -11
- data/spec/support/acceptance/helpers/rspec_helpers.rb +0 -24
- data/spec/support/acceptance/helpers/ruby_version_helpers.rb +0 -9
- data/spec/support/acceptance/helpers/step_helpers.rb +0 -127
- data/spec/support/acceptance/matchers/have_output.rb +0 -31
- data/spec/support/acceptance/matchers/indicate_number_of_tests_was_run_matcher.rb +0 -55
- data/spec/support/acceptance/matchers/indicate_that_tests_were_run_matcher.rb +0 -103
- data/spec/support/tests/bundle.rb +0 -94
- data/spec/support/tests/command_runner.rb +0 -230
- data/spec/support/tests/current_bundle.rb +0 -55
- data/spec/support/tests/database.rb +0 -28
- data/spec/support/tests/database_adapters/postgresql.rb +0 -25
- data/spec/support/tests/database_adapters/sqlite3.rb +0 -26
- data/spec/support/tests/database_configuration.rb +0 -33
- data/spec/support/tests/database_configuration_registry.rb +0 -28
- data/spec/support/tests/filesystem.rb +0 -100
- data/spec/support/tests/version.rb +0 -45
- data/spec/support/unit/active_record/create_table.rb +0 -54
- data/spec/support/unit/attribute.rb +0 -45
- data/spec/support/unit/capture.rb +0 -46
- data/spec/support/unit/change_value.rb +0 -111
- data/spec/support/unit/create_model_arguments/basic.rb +0 -135
- data/spec/support/unit/create_model_arguments/has_many.rb +0 -15
- data/spec/support/unit/create_model_arguments/uniqueness_matcher.rb +0 -74
- data/spec/support/unit/helpers/action_pack_versions.rb +0 -22
- data/spec/support/unit/helpers/active_model_helpers.rb +0 -27
- data/spec/support/unit/helpers/active_model_versions.rb +0 -32
- data/spec/support/unit/helpers/active_record_versions.rb +0 -44
- data/spec/support/unit/helpers/active_resource_builder.rb +0 -27
- data/spec/support/unit/helpers/allow_value_matcher_helpers.rb +0 -15
- data/spec/support/unit/helpers/class_builder.rb +0 -90
- data/spec/support/unit/helpers/column_type_helpers.rb +0 -26
- data/spec/support/unit/helpers/confirmation_matcher_helpers.rb +0 -17
- data/spec/support/unit/helpers/controller_builder.rb +0 -63
- data/spec/support/unit/helpers/database_helpers.rb +0 -20
- data/spec/support/unit/helpers/i18n_faker.rb +0 -15
- data/spec/support/unit/helpers/mailer_builder.rb +0 -12
- data/spec/support/unit/helpers/message_helpers.rb +0 -19
- data/spec/support/unit/helpers/model_builder.rb +0 -114
- data/spec/support/unit/helpers/rails_versions.rb +0 -42
- data/spec/support/unit/helpers/validation_matcher_scenario_helpers.rb +0 -44
- data/spec/support/unit/i18n.rb +0 -7
- data/spec/support/unit/load_environment.rb +0 -12
- data/spec/support/unit/matchers/deprecate.rb +0 -60
- data/spec/support/unit/matchers/fail_with_message_including_matcher.rb +0 -51
- data/spec/support/unit/matchers/fail_with_message_matcher.rb +0 -64
- data/spec/support/unit/matchers/print_warning_including.rb +0 -67
- data/spec/support/unit/model_creation_strategies/active_model.rb +0 -111
- data/spec/support/unit/model_creation_strategies/active_record.rb +0 -77
- data/spec/support/unit/model_creators.rb +0 -19
- data/spec/support/unit/model_creators/active_model.rb +0 -39
- data/spec/support/unit/model_creators/active_record.rb +0 -42
- data/spec/support/unit/model_creators/active_record/has_and_belongs_to_many.rb +0 -95
- data/spec/support/unit/model_creators/active_record/has_many.rb +0 -67
- data/spec/support/unit/model_creators/active_record/uniqueness_matcher.rb +0 -42
- data/spec/support/unit/model_creators/basic.rb +0 -102
- data/spec/support/unit/rails_application.rb +0 -151
- data/spec/support/unit/record_builder_with_i18n_validation_message.rb +0 -69
- data/spec/support/unit/record_validating_confirmation_builder.rb +0 -54
- data/spec/support/unit/record_with_different_error_attribute_builder.rb +0 -92
- data/spec/support/unit/shared_examples/ignoring_interference_by_writer.rb +0 -79
- data/spec/support/unit/shared_examples/numerical_submatcher.rb +0 -17
- data/spec/support/unit/shared_examples/set_session_or_flash.rb +0 -360
- data/spec/support/unit/validation_matcher_scenario.rb +0 -60
- data/spec/unit/shoulda/matchers/action_controller/callback_matcher_spec.rb +0 -82
- data/spec/unit/shoulda/matchers/action_controller/filter_param_matcher_spec.rb +0 -28
- data/spec/unit/shoulda/matchers/action_controller/permit_matcher_spec.rb +0 -629
- data/spec/unit/shoulda/matchers/action_controller/redirect_to_matcher_spec.rb +0 -42
- data/spec/unit/shoulda/matchers/action_controller/render_template_matcher_spec.rb +0 -76
- data/spec/unit/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +0 -62
- data/spec/unit/shoulda/matchers/action_controller/rescue_from_matcher_spec.rb +0 -90
- data/spec/unit/shoulda/matchers/action_controller/respond_with_matcher_spec.rb +0 -31
- data/spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb +0 -330
- data/spec/unit/shoulda/matchers/action_controller/route_params_spec.rb +0 -30
- data/spec/unit/shoulda/matchers/action_controller/set_flash_matcher_spec.rb +0 -67
- data/spec/unit/shoulda/matchers/action_controller/set_session_matcher_spec.rb +0 -17
- data/spec/unit/shoulda/matchers/action_controller/set_session_or_flash_matcher_spec.rb +0 -562
- data/spec/unit/shoulda/matchers/active_model/allow_mass_assignment_of_matcher_spec.rb +0 -117
- data/spec/unit/shoulda/matchers/active_model/allow_value_matcher_spec.rb +0 -829
- data/spec/unit/shoulda/matchers/active_model/disallow_value_matcher_spec.rb +0 -86
- data/spec/unit/shoulda/matchers/active_model/have_secure_password_matcher_spec.rb +0 -20
- data/spec/unit/shoulda/matchers/active_model/helpers_spec.rb +0 -162
- data/spec/unit/shoulda/matchers/active_model/validate_absence_of_matcher_spec.rb +0 -290
- data/spec/unit/shoulda/matchers/active_model/validate_acceptance_of_matcher_spec.rb +0 -109
- data/spec/unit/shoulda/matchers/active_model/validate_confirmation_of_matcher_spec.rb +0 -172
- data/spec/unit/shoulda/matchers/active_model/validate_exclusion_of_matcher_spec.rb +0 -264
- data/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb +0 -1049
- data/spec/unit/shoulda/matchers/active_model/validate_length_of_matcher_spec.rb +0 -335
- data/spec/unit/shoulda/matchers/active_model/validate_numericality_of_matcher_spec.rb +0 -1865
- data/spec/unit/shoulda/matchers/active_model/validate_presence_of_matcher_spec.rb +0 -406
- data/spec/unit/shoulda/matchers/active_record/accept_nested_attributes_for_matcher_spec.rb +0 -107
- data/spec/unit/shoulda/matchers/active_record/association_matcher_spec.rb +0 -1672
- data/spec/unit/shoulda/matchers/active_record/association_matchers/model_reflection_spec.rb +0 -251
- data/spec/unit/shoulda/matchers/active_record/define_enum_for_matcher_spec.rb +0 -690
- data/spec/unit/shoulda/matchers/active_record/have_db_column_matcher_spec.rb +0 -111
- data/spec/unit/shoulda/matchers/active_record/have_db_index_matcher_spec.rb +0 -85
- data/spec/unit/shoulda/matchers/active_record/have_readonly_attributes_matcher_spec.rb +0 -41
- data/spec/unit/shoulda/matchers/active_record/have_secure_token_matcher_spec.rb +0 -169
- data/spec/unit/shoulda/matchers/active_record/serialize_matcher_spec.rb +0 -86
- data/spec/unit/shoulda/matchers/active_record/validate_uniqueness_of_matcher_spec.rb +0 -1682
- data/spec/unit/shoulda/matchers/doublespeak/double_collection_spec.rb +0 -190
- data/spec/unit/shoulda/matchers/doublespeak/double_implementation_registry_spec.rb +0 -21
- data/spec/unit/shoulda/matchers/doublespeak/double_spec.rb +0 -271
- data/spec/unit/shoulda/matchers/doublespeak/object_double_spec.rb +0 -77
- data/spec/unit/shoulda/matchers/doublespeak/proxy_implementation_spec.rb +0 -72
- data/spec/unit/shoulda/matchers/doublespeak/stub_implementation_spec.rb +0 -101
- data/spec/unit/shoulda/matchers/doublespeak/world_spec.rb +0 -78
- data/spec/unit/shoulda/matchers/doublespeak_spec.rb +0 -27
- data/spec/unit/shoulda/matchers/independent/delegate_method_matcher/stubbed_target_spec.rb +0 -43
- data/spec/unit/shoulda/matchers/independent/delegate_method_matcher_spec.rb +0 -650
- data/spec/unit/shoulda/matchers/routing/route_matcher_spec.rb +0 -406
- data/spec/unit/shoulda/matchers/util/word_wrap_spec.rb +0 -252
- data/spec/unit_spec_helper.rb +0 -54
- data/spec/warnings_spy.rb +0 -64
- data/spec/warnings_spy/filesystem.rb +0 -45
- data/spec/warnings_spy/partitioner.rb +0 -36
- data/spec/warnings_spy/reader.rb +0 -53
- data/spec/warnings_spy/reporter.rb +0 -88
- data/tasks/documentation.rb +0 -199
- data/zeus.json +0 -11
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'unit_spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveModel::DisallowValueMatcher, type: :model do
|
4
|
-
context 'an attribute with a format validation' do
|
5
|
-
it 'does not match if the value is allowed' do
|
6
|
-
expect(validating_format(with: /abc/)).not_to matcher('abcde').for(:attr)
|
7
|
-
end
|
8
|
-
|
9
|
-
it 'matches if the value is not allowed' do
|
10
|
-
expect(validating_format(with: /abc/)).to matcher('xyz').for(:attr)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context "an attribute with a context-dependent validation" do
|
15
|
-
context "without the validation context" do
|
16
|
-
it "does not match" do
|
17
|
-
expect(validating_format(with: /abc/, on: :customisable)).not_to matcher("xyz").for(:attr)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "with the validation context" do
|
22
|
-
it "disallows a bad value" do
|
23
|
-
expect(validating_format(with: /abc/, on: :customisable)).to matcher("xyz").for(:attr).on(:customisable)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "does not match a good value" do
|
27
|
-
expect(validating_format(with: /abc/, on: :customisable)).not_to matcher("abcde").for(:attr).on(:customisable)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'an attribute with a format validation and a custom message' do
|
33
|
-
it 'does not match if the value and message are both correct' do
|
34
|
-
expect(validating_format(with: /abc/, message: 'good message')).
|
35
|
-
not_to matcher('abcde').for(:attr).with_message('good message')
|
36
|
-
end
|
37
|
-
|
38
|
-
it "delegates its failure message to its allow matcher's negative failure message" do
|
39
|
-
allow_matcher = double('allow_matcher',
|
40
|
-
failure_message_when_negated: 'allow matcher failure',
|
41
|
-
).as_null_object
|
42
|
-
allow(Shoulda::Matchers::ActiveModel::AllowValueMatcher).
|
43
|
-
to receive(:new).
|
44
|
-
and_return(allow_matcher)
|
45
|
-
|
46
|
-
matcher = matcher('abcde').for(:attr).with_message('good message')
|
47
|
-
matcher.matches?(validating_format(with: /abc/, message: 'good message'))
|
48
|
-
|
49
|
-
expect(matcher.failure_message).to eq 'allow matcher failure'
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'matches if the message is correct but the value is not' do
|
53
|
-
expect(validating_format(with: /abc/, message: 'good message')).
|
54
|
-
to matcher('xyz').for(:attr).with_message('good message')
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'an attribute where the message occurs on another attribute' do
|
59
|
-
it 'matches if the message is correct but the value is not' do
|
60
|
-
expect(record_with_custom_validation).to \
|
61
|
-
matcher('bad value').for(:attr).with_message(/some message/, against: :attr2)
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'does not match if the value and message are both correct' do
|
65
|
-
expect(record_with_custom_validation).not_to \
|
66
|
-
matcher('good value').for(:attr).with_message(/some message/, against: :attr2)
|
67
|
-
end
|
68
|
-
|
69
|
-
def record_with_custom_validation
|
70
|
-
define_model :example, attr: :string, attr2: :string do
|
71
|
-
validate :custom_validation
|
72
|
-
|
73
|
-
def custom_validation
|
74
|
-
if self[:attr] != 'good value'
|
75
|
-
self.errors[:attr2] << 'some message'
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end.new
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def matcher(value)
|
83
|
-
described_class.new(value)
|
84
|
-
end
|
85
|
-
alias_method :disallow_value, :matcher
|
86
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'unit_spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveModel::HaveSecurePasswordMatcher, type: :model do
|
4
|
-
if active_model_3_1?
|
5
|
-
it 'matches when the subject configures has_secure_password with default options' do
|
6
|
-
working_model = define_model(:example, password_digest: :string) { has_secure_password }
|
7
|
-
expect(working_model.new).to have_secure_password
|
8
|
-
end
|
9
|
-
|
10
|
-
it 'does not match when the subject does not authenticate a password' do
|
11
|
-
no_secure_password = define_model(:example)
|
12
|
-
expect(no_secure_password.new).not_to have_secure_password
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'does not match when the subject is missing the password_digest attribute' do
|
16
|
-
no_digest_column = define_model(:example) { has_secure_password }
|
17
|
-
expect(no_digest_column.new).not_to have_secure_password
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,162 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'unit_spec_helper'
|
3
|
-
|
4
|
-
describe Shoulda::Matchers::ActiveModel::Helpers do
|
5
|
-
include Shoulda::Matchers::ActiveModel
|
6
|
-
after { I18n.backend.reload! }
|
7
|
-
|
8
|
-
describe 'default_error_message' do
|
9
|
-
context 'if the translation for the model attribute’s error exists' do
|
10
|
-
it 'provides the right error message for validate_presence_of' do
|
11
|
-
store_translations
|
12
|
-
|
13
|
-
assert_presence_validation_has_correct_message
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'provides the right error message for validates_length_of' do
|
17
|
-
store_translations
|
18
|
-
|
19
|
-
assert_length_validation_has_correct_message
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'if no translation for the model attribute’s error exists' do
|
24
|
-
context 'and the translation for the model’s error exists' do
|
25
|
-
it 'provides the right error message for validate_presence_of' do
|
26
|
-
store_translations(without: :model_attribute)
|
27
|
-
|
28
|
-
assert_presence_validation_has_correct_message
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'provides the right error message for validates_length_of' do
|
32
|
-
store_translations(without: :model_attribute)
|
33
|
-
|
34
|
-
assert_length_validation_has_correct_message
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'and no translation for the model’s error exists' do
|
39
|
-
context 'and the translation for the message exists' do
|
40
|
-
it 'provides the right error message for validate_presence_of' do
|
41
|
-
store_translations(without: [:model_attribute, :model])
|
42
|
-
|
43
|
-
assert_presence_validation_has_correct_message
|
44
|
-
end
|
45
|
-
|
46
|
-
it 'provides the right error message for validates_length_of' do
|
47
|
-
store_translations(without: [:model_attribute, :model])
|
48
|
-
|
49
|
-
assert_length_validation_has_correct_message
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'and no translation for the message exists' do
|
54
|
-
context 'and the translation for the attribute exists' do
|
55
|
-
it 'provides the right error message for validate_presence_of' do
|
56
|
-
store_translations(without: [:model_attribute, :model, :message])
|
57
|
-
|
58
|
-
assert_presence_validation_has_correct_message
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'provides the right error message for validates_length_of' do
|
62
|
-
store_translations(without: [:model_attribute, :model, :message])
|
63
|
-
|
64
|
-
assert_length_validation_has_correct_message
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'and no translation for the attribute exists' do
|
69
|
-
it 'provides the general error message for validate_presence_of' do
|
70
|
-
assert_presence_validation_has_correct_message
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'provides the general error message for validates_length_of' do
|
74
|
-
assert_length_validation_has_correct_message
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context 'if ActiveModel::Errors#generate_message behavior has changed' do
|
82
|
-
it 'provides the right error message for validate_presence_of' do
|
83
|
-
stub_active_model_message_generation(type: :blank,
|
84
|
-
message: 'Behavior has diverged.')
|
85
|
-
assert_presence_validation_has_correct_message
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def assert_presence_validation_has_correct_message
|
91
|
-
record = define_model :example, attr: :string do
|
92
|
-
validates_presence_of :attr
|
93
|
-
end.new
|
94
|
-
expect(record).to validate_presence_of(:attr)
|
95
|
-
end
|
96
|
-
|
97
|
-
def assert_length_validation_has_correct_message
|
98
|
-
record = define_model :example, attr: :string do
|
99
|
-
validates_length_of :attr, is: 40, allow_blank: true
|
100
|
-
end.new
|
101
|
-
expect(record).to validate_length_of(:attr).is_equal_to(40)
|
102
|
-
end
|
103
|
-
|
104
|
-
def store_translations(options = {without: []})
|
105
|
-
options[:without] = Array.wrap(options[:without] || [])
|
106
|
-
|
107
|
-
translations = {
|
108
|
-
activerecord: {
|
109
|
-
errors: {
|
110
|
-
models: {
|
111
|
-
example: {
|
112
|
-
attributes: {
|
113
|
-
attr: {}
|
114
|
-
}
|
115
|
-
}
|
116
|
-
},
|
117
|
-
messages: {}
|
118
|
-
}
|
119
|
-
},
|
120
|
-
errors: {
|
121
|
-
attributes: {
|
122
|
-
attr: {}
|
123
|
-
},
|
124
|
-
messages: {}
|
125
|
-
}
|
126
|
-
}
|
127
|
-
|
128
|
-
unless options[:without].include?(:model_attribute)
|
129
|
-
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = "Don't you do that to me!"
|
130
|
-
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:wrong_length] = "Don't you do that to me!"
|
131
|
-
end
|
132
|
-
|
133
|
-
unless options[:without].include?(:model)
|
134
|
-
translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!'
|
135
|
-
translations[:activerecord][:errors][:models][:example][:wrong_length] = 'Give it one more try!'
|
136
|
-
end
|
137
|
-
|
138
|
-
unless options[:without].include?(:message)
|
139
|
-
translations[:activerecord][:errors][:messages][:blank] = 'Oh no!'
|
140
|
-
translations[:activerecord][:errors][:messages][:wrong_length] = 'Oh no!'
|
141
|
-
end
|
142
|
-
|
143
|
-
unless options[:without].include?(:attribute)
|
144
|
-
translations[:errors][:attributes][:attr][:blank] = 'Seriously?'
|
145
|
-
translations[:errors][:attributes][:attr][:wrong_length] = 'Seriously?'
|
146
|
-
end
|
147
|
-
|
148
|
-
I18n.backend.store_translations(:en, translations)
|
149
|
-
end
|
150
|
-
|
151
|
-
def stub_active_model_message_generation(options = {})
|
152
|
-
attribute = options.delete(:attribute) || :attr
|
153
|
-
message = options.delete(:message)
|
154
|
-
type = options.delete(:type)
|
155
|
-
|
156
|
-
expect_any_instance_of(ActiveModel::Errors).
|
157
|
-
to receive(:generate_message).
|
158
|
-
with(attribute, type, {}).
|
159
|
-
at_least(1).
|
160
|
-
and_return(message)
|
161
|
-
end
|
162
|
-
end
|
@@ -1,290 +0,0 @@
|
|
1
|
-
require 'unit_spec_helper'
|
2
|
-
|
3
|
-
describe Shoulda::Matchers::ActiveModel::ValidateAbsenceOfMatcher, type: :model do
|
4
|
-
if active_model_supports_absence_validation?
|
5
|
-
def self.available_column_types
|
6
|
-
[
|
7
|
-
:string,
|
8
|
-
:text,
|
9
|
-
:integer,
|
10
|
-
:float,
|
11
|
-
:decimal,
|
12
|
-
:datetime,
|
13
|
-
:timestamp,
|
14
|
-
:time,
|
15
|
-
:date,
|
16
|
-
:binary
|
17
|
-
]
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'a model with an absence validation' do
|
21
|
-
it 'accepts' do
|
22
|
-
expect(validating_absence_of(:attr)).to validate_absence_of(:attr)
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'does not override the default message with a present' do
|
26
|
-
expect(validating_absence_of(:attr)).to validate_absence_of(:attr).with_message(nil)
|
27
|
-
end
|
28
|
-
|
29
|
-
available_column_types.each do |type|
|
30
|
-
context "when column is of type #{type}" do
|
31
|
-
it "accepts" do
|
32
|
-
expect(validating_absence_of(:attr, {}, type: type)).
|
33
|
-
to validate_absence_of(:attr)
|
34
|
-
end
|
35
|
-
|
36
|
-
it_supports(
|
37
|
-
'ignoring_interference_by_writer',
|
38
|
-
tests: {
|
39
|
-
accept_if_qualified_but_changing_value_does_not_interfere: {
|
40
|
-
changing_values_with: :next_value
|
41
|
-
},
|
42
|
-
}
|
43
|
-
)
|
44
|
-
|
45
|
-
define_method(:validation_matcher_scenario_args) do |*args|
|
46
|
-
super(*args).deep_merge(column_type: type)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context 'when used in the negative' do
|
52
|
-
it 'fails' do
|
53
|
-
assertion = lambda do
|
54
|
-
expect(validating_absence_of(:attr)).
|
55
|
-
not_to validate_absence_of(:attr)
|
56
|
-
end
|
57
|
-
|
58
|
-
message = <<-MESSAGE
|
59
|
-
Expected Example not to validate that :attr is empty/falsy, but this
|
60
|
-
could not be proved.
|
61
|
-
After setting :attr to ‹"an arbitrary value"›, the matcher expected
|
62
|
-
the Example to be valid, but it was invalid instead, producing these
|
63
|
-
validation errors:
|
64
|
-
|
65
|
-
* attr: ["must be blank"]
|
66
|
-
MESSAGE
|
67
|
-
|
68
|
-
expect(&assertion).to fail_with_message(message)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def validation_matcher_scenario_args
|
73
|
-
super.deep_merge(model_creator: :active_record)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
context 'a model without an absence validation' do
|
78
|
-
it 'rejects with the correct failure message' do
|
79
|
-
record = define_model(:example, attr: :string).new
|
80
|
-
|
81
|
-
message = <<-MESSAGE
|
82
|
-
Expected Example to validate that :attr is empty/falsy, but this could
|
83
|
-
not be proved.
|
84
|
-
After setting :attr to ‹"an arbitrary value"›, the matcher expected
|
85
|
-
the Example to be invalid, but it was valid instead.
|
86
|
-
MESSAGE
|
87
|
-
|
88
|
-
assertion = lambda do
|
89
|
-
expect(record).to validate_absence_of(:attr)
|
90
|
-
end
|
91
|
-
|
92
|
-
expect(&assertion).to fail_with_message(message)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
context 'an ActiveModel class with an absence validation' do
|
97
|
-
it 'accepts' do
|
98
|
-
expect(active_model_validating_absence_of(:attr)).to validate_absence_of(:attr)
|
99
|
-
end
|
100
|
-
|
101
|
-
it 'does not override the default message with a blank' do
|
102
|
-
expect(active_model_validating_absence_of(:attr)).to validate_absence_of(:attr).with_message(nil)
|
103
|
-
end
|
104
|
-
|
105
|
-
it_supports(
|
106
|
-
'ignoring_interference_by_writer',
|
107
|
-
tests: {
|
108
|
-
accept_if_qualified_but_changing_value_does_not_interfere: {
|
109
|
-
changing_values_with: :upcase
|
110
|
-
},
|
111
|
-
}
|
112
|
-
)
|
113
|
-
|
114
|
-
def validation_matcher_scenario_args
|
115
|
-
super.deep_merge(model_creator: :active_model)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
context 'an ActiveModel class without an absence validation' do
|
120
|
-
it 'rejects with the correct failure message' do
|
121
|
-
message = <<-MESSAGE
|
122
|
-
Expected Example to validate that :attr is empty/falsy, but this could
|
123
|
-
not be proved.
|
124
|
-
After setting :attr to ‹"an arbitrary value"›, the matcher expected
|
125
|
-
the Example to be invalid, but it was valid instead.
|
126
|
-
MESSAGE
|
127
|
-
|
128
|
-
assertion = lambda do
|
129
|
-
expect(active_model_with(:attr)).to validate_absence_of(:attr)
|
130
|
-
end
|
131
|
-
|
132
|
-
expect(&assertion).to fail_with_message(message)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
context 'a has_many association with an absence validation' do
|
137
|
-
it 'requires the attribute to not be set' do
|
138
|
-
expect(having_many(:children, absence: true)).to validate_absence_of(:children)
|
139
|
-
end
|
140
|
-
|
141
|
-
it_supports(
|
142
|
-
'ignoring_interference_by_writer',
|
143
|
-
tests: {
|
144
|
-
accept_if_qualified_but_changing_value_does_not_interfere: {
|
145
|
-
changing_values_with: :next_value
|
146
|
-
},
|
147
|
-
}
|
148
|
-
)
|
149
|
-
|
150
|
-
def validation_matcher_scenario_args
|
151
|
-
super.deep_merge(model_creator: :"active_record/has_many")
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
context 'a has_many association without an absence validation' do
|
156
|
-
it 'does not require the attribute to not be set' do
|
157
|
-
expect(having_many(:children, absence: false)).
|
158
|
-
not_to validate_absence_of(:children)
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
context 'an absent has_and_belongs_to_many association' do
|
163
|
-
it 'accepts' do
|
164
|
-
model = having_and_belonging_to_many(:children, absence: true)
|
165
|
-
expect(model).to validate_absence_of(:children)
|
166
|
-
end
|
167
|
-
|
168
|
-
it_supports(
|
169
|
-
'ignoring_interference_by_writer',
|
170
|
-
tests: {
|
171
|
-
accept_if_qualified_but_changing_value_does_not_interfere: {
|
172
|
-
changing_values_with: :next_value
|
173
|
-
},
|
174
|
-
}
|
175
|
-
)
|
176
|
-
|
177
|
-
def validation_matcher_scenario_args
|
178
|
-
super.deep_merge(model_creator: :"active_record/habtm")
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
context 'a non-absent has_and_belongs_to_many association' do
|
183
|
-
it 'rejects with the correct failure message' do
|
184
|
-
model = having_and_belonging_to_many(:children, absence: false)
|
185
|
-
|
186
|
-
message = <<-MESSAGE
|
187
|
-
Expected Parent to validate that :children is empty/falsy, but this
|
188
|
-
could not be proved.
|
189
|
-
After setting :children to ‹[#<Child id: nil>]›, the matcher expected
|
190
|
-
the Parent to be invalid, but it was valid instead.
|
191
|
-
MESSAGE
|
192
|
-
|
193
|
-
assertion = lambda do
|
194
|
-
expect(model).to validate_absence_of(:children)
|
195
|
-
end
|
196
|
-
|
197
|
-
expect(&assertion).to fail_with_message(message)
|
198
|
-
end
|
199
|
-
end
|
200
|
-
|
201
|
-
context "an i18n translation containing %{attribute} and %{model}" do
|
202
|
-
after { I18n.backend.reload! }
|
203
|
-
|
204
|
-
it "does not raise an exception" do
|
205
|
-
stub_translation("activerecord.errors.messages.present",
|
206
|
-
"%{attribute} must be blank in a %{model}")
|
207
|
-
|
208
|
-
expect {
|
209
|
-
expect(validating_absence_of(:attr)).to validate_absence_of(:attr)
|
210
|
-
}.to_not raise_exception
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
context "an attribute with a context-dependent validation" do
|
215
|
-
context "without the validation context" do
|
216
|
-
it "does not match" do
|
217
|
-
expect(validating_absence_of(:attr, on: :customisable)).not_to validate_absence_of(:attr)
|
218
|
-
end
|
219
|
-
end
|
220
|
-
|
221
|
-
context "with the validation context" do
|
222
|
-
it "matches" do
|
223
|
-
expect(validating_absence_of(:attr, on: :customisable)).to validate_absence_of(:attr).on(:customisable)
|
224
|
-
end
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
def define_model_validating_absence_of(attr, validation_options = {}, given_column_options = {})
|
229
|
-
default_column_options = { type: :string, options: {} }
|
230
|
-
column_options = default_column_options.merge(given_column_options)
|
231
|
-
|
232
|
-
define_model :example, attr => column_options do |model|
|
233
|
-
model.validates_absence_of(attr, validation_options)
|
234
|
-
|
235
|
-
if block_given?
|
236
|
-
yield model
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
def validating_absence_of(attr, validation_options = {}, given_column_options = {})
|
242
|
-
model = define_model_validating_absence_of(
|
243
|
-
attr,
|
244
|
-
validation_options,
|
245
|
-
given_column_options
|
246
|
-
)
|
247
|
-
model.new
|
248
|
-
end
|
249
|
-
alias_method :build_record_validating_absence_of, :validating_absence_of
|
250
|
-
|
251
|
-
def active_model_with(attr, &block)
|
252
|
-
define_active_model_class('Example', accessors: [attr], &block).new
|
253
|
-
end
|
254
|
-
|
255
|
-
def active_model_validating_absence_of(attr)
|
256
|
-
active_model_with(attr) do
|
257
|
-
validates_absence_of attr
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
def having_many(plural_name, options = {})
|
262
|
-
define_model plural_name.to_s.singularize
|
263
|
-
define_model :parent do
|
264
|
-
has_many plural_name
|
265
|
-
if options[:absence]
|
266
|
-
validates_absence_of plural_name
|
267
|
-
end
|
268
|
-
end.new
|
269
|
-
end
|
270
|
-
|
271
|
-
def having_and_belonging_to_many(plural_name, options = {})
|
272
|
-
create_table 'children_parents', id: false do |t|
|
273
|
-
t.integer "#{plural_name.to_s.singularize}_id"
|
274
|
-
t.integer :parent_id
|
275
|
-
end
|
276
|
-
|
277
|
-
define_model plural_name.to_s.singularize
|
278
|
-
define_model :parent do
|
279
|
-
has_and_belongs_to_many plural_name
|
280
|
-
if options[:absence]
|
281
|
-
validates_absence_of plural_name
|
282
|
-
end
|
283
|
-
end.new
|
284
|
-
end
|
285
|
-
|
286
|
-
def validation_matcher_scenario_args
|
287
|
-
super.deep_merge(matcher_name: :validate_absence_of)
|
288
|
-
end
|
289
|
-
end
|
290
|
-
end
|