Sha256: 3f86c820d110f45ead4cf9d70542bfadc230e09a952b2e518c6c1f0feb5c46cf

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module HaveErrorMatchers
  class HaveError
    def initialize(ya, *type_and_opts)
      @ya       = ya
      @options  = type_and_opts.extract_options!
      @type     = type_and_opts.shift || :invalid
    end

    def on(attribute)
      @attribute = attribute
      return self
    end

    def matches?(record)
      @record = record
      @record.send(:run_validations!) unless @options.delete(:skip_validate)
      @record.errors[@attribute].include?(@record.errors.generate_message(@attribute, @type, @options))
    end

    def failure_message_for_should
      "Expected #{@record.class.model_name.human} record to have an error of type :#@type for attribute #{@record.class.human_attribute_name(@attribute)}."
    end

    def failure_message_for_should_not
      "Expected #{@record.class.model_name.human} record to NOT have an error of type :#@type for attribute #{@record.class.human_attribute_name(@attribute)}."
    end

    def description
      ["have a :#@type validation error",
       *("on :#@attribute" if @attribute)].join(' ')
    end
  end

  def have_error(*type_and_opts)
    HaveError.new(true, *type_and_opts)
  end

  # RSpec::Matchers.define :have_error do |*type_and_opts|
  #   match do |actual|
  #     opts = type_and_opts.extract_options!
  #     type = type_and_opts.shift || :invalid
  #     actual.send(:run_validations!) unless opts.delete(:skip_validate)
  #     actual.errors # WIP
  #   end
  # end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
personhood-0.2.0 spec/support/have_error_matchers.rb
personhood-0.1.2 spec/support/have_error_matchers.rb