davydovanton/rspec-hanami

Form matcher's failure message unexpected behaviour

ADodulad opened this issue · 1 comments

Ok, I would actually address 3 issues here but since they all related to the same method they might have a single solution.

First

Each matcher includes following code:
failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{method} method param" }
Anytime any of the matchers fails I receive the following error:

Failure/Error: it { expect(view.form).to have_form_field(node: "input", type: "password") }
   FrozenError: can't modify frozen String

This error comes from the #{form.inspect} operation when RSpec generates failure message and originates in haml's inspect method (which calls gsub! with a frozen string option enabled).

I was not able to reproduce this error using rspec-hanami repo and haml though. However I'm sure I'm just missing out something that happens under Hanami's hood.

Second

Whenever form matcher fail failure_message would output multi page diff of a FormBuilder object which is overkill.

Third

have_field matcher's failure_message includes errors. Look at that, there is no #{form.inspect}, #{attributes} or #{method} defined within matcher. Obviously that's must be a copy-paste issue from 'have_method' matcher.

matcher :have_field do |params|
  require 'hanami/utils/hash'
  attr_reader :form, :form_data, :params

  description { "have field with params" }
  match do |form|
    @form = form
    @params = ::Hanami::Utils::Hash.new(params).symbolize!
    @form_data = RSpec::Hanami::FormParser.new.call(form.to_s)

    form_data.any? do |input|
      input.merge(params) == params.merge(input)
    end
  end

  failure_message { |actual| "expect #{form.inspect} (#{attributes}) to have #{method} method param" }
  diffable
end

Solution

So as I mentioned one approach might handle all three issues:

  1. Output @form_data = RSpec::Hanami::FormParser.new.call(form.to_s) instead of '#{form.inspect}' in `failure_message' of every matcher.
  2. Remove diffable for every form matcher.
  3. Fix have_field matcher's failure_message with params.

PR #25 as a proposal