thoughtbot--shoulda-matchers/spec/unit/shoulda/matchers/active_model/helpers_spec.rb

163 lines
5.4 KiB
Ruby
Raw Normal View History

# encoding: UTF-8
require 'unit_spec_helper'
describe Shoulda::Matchers::ActiveModel::Helpers do
2012-03-23 11:12:09 -04:00
include Shoulda::Matchers::ActiveModel
2012-12-20 00:04:27 -05:00
after { I18n.backend.reload! }
2012-03-23 11:12:09 -04:00
2012-12-20 00:04:27 -05:00
describe 'default_error_message' do
context 'if the translation for the model attributes error exists' do
it 'provides the right error message for validate_presence_of' do
store_translations
2012-12-20 00:04:27 -05:00
assert_presence_validation_has_correct_message
end
2012-12-20 00:04:27 -05:00
it 'provides the right error message for validates_length_of' do
store_translations
2012-12-20 00:04:27 -05:00
assert_length_validation_has_correct_message
end
end
2012-03-16 12:15:23 -04:00
2012-12-20 00:04:27 -05:00
context 'if no translation for the model attributes error exists' do
context 'and the translation for the models error exists' do
it 'provides the right error message for validate_presence_of' do
2014-01-17 15:20:44 -05:00
store_translations(without: :model_attribute)
2012-12-20 00:04:27 -05:00
assert_presence_validation_has_correct_message
end
2012-12-20 00:04:27 -05:00
it 'provides the right error message for validates_length_of' do
2014-01-17 15:20:44 -05:00
store_translations(without: :model_attribute)
2012-12-20 00:04:27 -05:00
assert_length_validation_has_correct_message
end
end
2012-03-16 12:15:23 -04:00
2012-12-20 00:04:27 -05:00
context 'and no translation for the models error exists' do
context 'and the translation for the message exists' do
it 'provides the right error message for validate_presence_of' do
2014-01-17 15:20:44 -05:00
store_translations(without: [:model_attribute, :model])
2012-12-20 00:04:27 -05:00
assert_presence_validation_has_correct_message
end
2012-12-20 00:04:27 -05:00
it 'provides the right error message for validates_length_of' do
2014-01-17 15:20:44 -05:00
store_translations(without: [:model_attribute, :model])
2012-12-20 00:04:27 -05:00
assert_length_validation_has_correct_message
end
end
2012-03-16 12:15:23 -04:00
2012-12-20 00:04:27 -05:00
context 'and no translation for the message exists' do
context 'and the translation for the attribute exists' do
it 'provides the right error message for validate_presence_of' do
2014-01-17 15:20:44 -05:00
store_translations(without: [:model_attribute, :model, :message])
2012-12-20 00:04:27 -05:00
assert_presence_validation_has_correct_message
end
2012-12-20 00:04:27 -05:00
it 'provides the right error message for validates_length_of' do
2014-01-17 15:20:44 -05:00
store_translations(without: [:model_attribute, :model, :message])
2012-12-20 00:04:27 -05:00
assert_length_validation_has_correct_message
end
end
2012-03-16 12:15:23 -04:00
2012-12-20 00:04:27 -05:00
context 'and no translation for the attribute exists' do
it 'provides the general error message for validate_presence_of' do
assert_presence_validation_has_correct_message
end
2012-12-20 00:04:27 -05:00
it 'provides the general error message for validates_length_of' do
assert_length_validation_has_correct_message
end
end
end
end
end
context 'if ActiveModel::Errors#generate_message behavior has changed' do
it 'provides the right error message for validate_presence_of' do
2014-01-17 15:20:44 -05:00
stub_active_model_message_generation(type: :blank,
message: 'Behavior has diverged.')
assert_presence_validation_has_correct_message
end
end
end
2012-12-20 00:04:27 -05:00
def assert_presence_validation_has_correct_message
2014-01-17 17:01:43 -05:00
record = define_model :example, attr: :string do
2012-12-20 00:04:27 -05:00
validates_presence_of :attr
2014-01-17 17:01:43 -05:00
end.new
expect(record).to validate_presence_of(:attr)
2012-12-20 00:04:27 -05:00
end
def assert_length_validation_has_correct_message
2014-01-17 17:01:43 -05:00
record = define_model :example, attr: :string do
2014-01-17 15:20:44 -05:00
validates_length_of :attr, is: 40, allow_blank: true
2014-01-17 17:01:43 -05:00
end.new
2014-12-16 01:01:24 -05:00
expect(record).to validate_length_of(:attr).is_equal_to(40)
2012-12-20 00:04:27 -05:00
end
2014-01-17 15:20:44 -05:00
def store_translations(options = {without: []})
2012-12-20 00:04:27 -05:00
options[:without] = Array.wrap(options[:without] || [])
translations = {
2014-01-17 15:20:44 -05:00
activerecord: {
errors: {
models: {
example: {
attributes: {
attr: {}
2012-12-20 00:04:27 -05:00
}
}
},
2014-01-17 15:20:44 -05:00
messages: {}
2012-12-20 00:04:27 -05:00
}
},
2014-01-17 15:20:44 -05:00
errors: {
attributes: {
attr: {}
2012-12-20 00:04:27 -05:00
},
2014-01-17 15:20:44 -05:00
messages: {}
2012-12-20 00:04:27 -05:00
}
}
unless options[:without].include?(:model_attribute)
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = "Don't you do that to me!"
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:wrong_length] = "Don't you do that to me!"
end
unless options[:without].include?(:model)
translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!'
translations[:activerecord][:errors][:models][:example][:wrong_length] = 'Give it one more try!'
end
unless options[:without].include?(:message)
translations[:activerecord][:errors][:messages][:blank] = 'Oh no!'
translations[:activerecord][:errors][:messages][:wrong_length] = 'Oh no!'
end
unless options[:without].include?(:attribute)
translations[:errors][:attributes][:attr][:blank] = 'Seriously?'
translations[:errors][:attributes][:attr][:wrong_length] = 'Seriously?'
end
I18n.backend.store_translations(:en, translations)
end
def stub_active_model_message_generation(options = {})
attribute = options.delete(:attribute) || :attr
message = options.delete(:message)
type = options.delete(:type)
2014-11-07 17:22:20 -05:00
expect_any_instance_of(ActiveModel::Errors).
to receive(:generate_message).
with(attribute, type, {}).
at_least(1).
and_return(message)
end
2012-03-16 12:15:23 -04:00
end