Create have_error_on.rb

This commit is contained in:
Justin Edmund 2025-02-10 05:35:55 -08:00
parent e5ece0a7a3
commit c375292525

View 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