From c375292525aa8e6e0ac620a027114bfb5b680360 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 10 Feb 2025 05:35:55 -0800 Subject: [PATCH] Create have_error_on.rb --- spec/support/matchers/have_error_on.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spec/support/matchers/have_error_on.rb 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