-
-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ValidationMessageFinder from AllowValueMatcher
- Loading branch information
Showing
4 changed files
with
192 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
lib/shoulda/matchers/active_model/validation_message_finder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActiveModel | ||
|
||
# Finds message information from a model's #errors method. | ||
class ValidationMessageFinder | ||
include Helpers | ||
|
||
def initialize(instance, attribute) | ||
@instance = instance | ||
@attribute = attribute | ||
end | ||
|
||
def allow_description(allowed_values) | ||
"allow #{@attribute} to be set to #{allowed_values}" | ||
end | ||
|
||
def expected_message_from(attribute_message) | ||
attribute_message | ||
end | ||
|
||
def has_messages? | ||
errors.present? | ||
end | ||
|
||
def source_description | ||
'errors' | ||
end | ||
|
||
def messages_description | ||
if errors.empty? | ||
"no errors" | ||
else | ||
"errors: #{pretty_error_messages(validated_instance)}" | ||
end | ||
end | ||
|
||
def messages | ||
Array.wrap(messages_for_attribute) | ||
end | ||
|
||
private | ||
|
||
def messages_for_attribute | ||
if errors.respond_to?(:[]) | ||
errors[@attribute] | ||
else | ||
errors.on(@attribute) | ||
end | ||
end | ||
|
||
def errors | ||
validated_instance.errors | ||
end | ||
|
||
def validated_instance | ||
@validated_instance ||= validate_instance | ||
end | ||
|
||
def validate_instance | ||
@instance.valid? | ||
@instance | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
105 changes: 105 additions & 0 deletions
105
spec/shoulda/active_model/validation_message_finder_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
require 'spec_helper' | ||
|
||
describe Shoulda::Matchers::ActiveModel::ValidationMessageFinder do | ||
context '#allow_description' do | ||
it 'describes its attribute' do | ||
finder = build_finder(:attribute => :attr) | ||
|
||
description = finder.allow_description('allowed values') | ||
|
||
description.should == 'allow attr to be set to allowed values' | ||
end | ||
end | ||
|
||
context '#expected_message_from' do | ||
it 'returns the message as-is' do | ||
finder = build_finder | ||
|
||
message = finder.expected_message_from('some message') | ||
|
||
message.should == 'some message' | ||
end | ||
end | ||
|
||
context '#has_messages?' do | ||
it 'has messages when some validations fail' do | ||
finder = build_finder(:format => /abc/, :value => 'xyz') | ||
|
||
result = finder.has_messages? | ||
|
||
result.should be_true | ||
end | ||
|
||
it 'has no messages when all validations pass' do | ||
finder = build_finder(:format => /abc/, :value => 'abc') | ||
|
||
result = finder.has_messages? | ||
|
||
result.should be_false | ||
end | ||
end | ||
|
||
context '#messages' do | ||
it 'returns errors for the given attribute' do | ||
finder = build_finder(:format => /abc/, :value => 'xyz') | ||
|
||
messages = finder.messages | ||
|
||
messages.should == ['is invalid'] | ||
end | ||
end | ||
|
||
context '#messages_description' do | ||
it 'describes errors for the given attribute' do | ||
finder = build_finder( | ||
:attribute => :attr, | ||
:format => /abc/, | ||
:value => 'xyz' | ||
) | ||
|
||
description = finder.messages_description | ||
|
||
description.should == 'errors: ["attr is invalid (\"xyz\")"]' | ||
end | ||
|
||
it 'describes errors when there are none' do | ||
finder = build_finder(:format => /abc/, :value => 'abc') | ||
|
||
description = finder.messages_description | ||
|
||
description.should == 'no errors' | ||
end | ||
end | ||
|
||
context '#source_description' do | ||
it 'describes the source of its messages' do | ||
finder = build_finder | ||
|
||
description = finder.source_description | ||
|
||
description.should == 'errors' | ||
end | ||
end | ||
|
||
def build_finder(arguments = {}) | ||
arguments[:attribute] ||= :attr | ||
instance = build_instance_validating( | ||
arguments[:attribute], | ||
arguments[:format] || /abc/, | ||
arguments[:value] || 'abc' | ||
) | ||
Shoulda::Matchers::ActiveModel::ValidationMessageFinder.new( | ||
instance, | ||
arguments[:attribute] | ||
) | ||
end | ||
|
||
def build_instance_validating(attribute, format, value) | ||
model_class = define_model(:example, attribute => :string) do | ||
attr_accessible attribute | ||
validates_format_of attribute, :with => format | ||
end | ||
|
||
model_class.new(attribute => value) | ||
end | ||
end |