Sha256: 28cb670274031af3a3266429b15bb142c8a0c12c44f09c76718a27b4f886b024

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

RSpec::Matchers.define :update_record do |resource, attributes|
  supports_block_expectations

  match do |actual|
    if using_form?
      allow(form_klass).to receive(:new).and_call_original
      expect_any_instance_of(form_klass).to receive(:save).and_call_original
    end

    first_attribute_key = attributes.keys.first
    initial_matcher = change { resource.reload.public_send(first_attribute_key) }.to(attributes[first_attribute_key])
    update_all_attributes = attributes.except(first_attribute_key).inject(initial_matcher) do |current_matcher, (attribute_name, expected_value)|
      current_matcher.and change { resource.public_send(attribute_name) }.to(expected_value)
    end

    expect { actual.call }.to update_all_attributes

    if using_form?
      expect(form_klass).to have_received(:new).with(have_attributes(id: resource.id), kind_of(Hash))
    end
  end

  def using_form(form_klass)
    @form_klass = form_klass
    self
  end

  def using_form?
    form_klass.present?
  end

  private

  attr_reader :form_klass, :form
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-controller-matchers-0.0.2 lib/rspec/matchers/update_record_matcher.rb