Create have_error_on.rb
This commit is contained in:
parent
e5ece0a7a3
commit
c375292525
1 changed files with 12 additions and 0 deletions
12
spec/support/matchers/have_error_on.rb
Normal file
12
spec/support/matchers/have_error_on.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# This custom matcher checks that a model's errors on a given attribute include a specific phrase.
|
||||||
|
RSpec::Matchers.define :have_error_on do |attribute, expected_phrase|
|
||||||
|
match do |model|
|
||||||
|
model.valid? && model.errors[attribute].any? { |msg| msg.include?(expected_phrase) }
|
||||||
|
end
|
||||||
|
|
||||||
|
failure_message do |model|
|
||||||
|
"expected errors on #{attribute} to include '#{expected_phrase}', but got: #{model.errors[attribute].join(', ')}"
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue