Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create against method in allow_value matcher #1543

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/shoulda/matchers/active_model/allow_value_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,36 @@ module ActiveModel
# on(:create)
# end
#
# ##### against
#
# Use `against` if the validation is on an attribute
# other than the attribute being validated:
#
# class UserProfile
# include ActiveModel::Model
# attr_accessor :website_url
#
# alias_attribute :url, :website_url
#
# validates_format_of :url, with: URI.regexp
# end
#
# # RSpec
# RSpec.describe UserProfile, type: :model do
# it do
# should allow_value('https://foo.com').
# for(:website_url).
# against(:url)
# end
# end
#
# # Minitest (Shoulda)
# class UserProfileTest < ActiveSupport::TestCase
# should allow_value('https://foo.com').
# for(:website_url).
# against(:url)
# end
#
# ##### with_message
#
# Use `with_message` if you are using a custom validation message.
Expand Down Expand Up @@ -349,6 +379,12 @@ def on(context)
self
end

def against(attribute)
@attribute_to_check_message_against = attribute if attribute.present?

self
end

def with_message(message, given_options = {})
if message.present?
@expects_custom_validation_message = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,18 @@
end
end

context 'when the attribute that receives the validation error is provided' do
context 'given a valid value' do
it 'accepts' do
builder = builder_for_record_with_different_error_attribute
expect(builder.record).
to allow_value(builder.valid_value).
for(builder.attribute_to_validate).
against(builder.attribute_that_receives_error)
end
end
end

context 'when the validation error message was provided directly' do
context 'given a valid value' do
it 'accepts' do
Expand Down