diff --git a/spec/support/matchers/have_error_on.rb b/spec/support/matchers/have_error_on.rb new file mode 100644 index 0000000..0919cd1 --- /dev/null +++ b/spec/support/matchers/have_error_on.rb @@ -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