Module: Amount::ActiveRecord::RSpec::Matchers

Defined in:
lib/amount/active_record/rspec/matchers.rb

Overview

ActiveRecord-specific matcher definitions. Companion to ‘Amount::RSpec::Matchers` which holds the gem-core matchers.

Class Method Summary collapse

Class Method Details

.define_amount_column_matcherObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/amount/active_record/rspec/matchers.rb', line 11

def define_amount_column_matcher
  ::RSpec::Matchers.define :have_amount_column do |name, *expected_arguments|
    match do |record|
      @name = name
      @expected = Amount::RSpec::Support.coerce_amount_arguments(expected_arguments)
      @definition = record.class.amount_attribute_definitions.fetch(name.to_sym)

      @definition.read(record) == @expected &&
        record.public_send(@definition.atomic_column).to_i == @expected.atomic &&
        (@definition.fixed_symbol? ||
          record.public_send(@definition.symbol_column) == @expected.symbol.to_s)
    end

    failure_message do |record|
      "expected #{record.inspect} to have #{@name} column matching #{@expected.inspect}"
    end
  end
end

.define_amount_sum_matcherObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/amount/active_record/rspec/matchers.rb', line 30

def define_amount_sum_matcher
  ::RSpec::Matchers.define :match_amounts do |expected_hash|
    match do |actual_hash|
      @expected = expected_hash.to_h do |symbol, value|
        amount = Amount.new(value, symbol)
        [amount.symbol, amount]
      end
      @actual = Amount::RSpec::Support.normalize_amount_sums(actual_hash)

      @actual == @expected
    end

    failure_message do |_actual_hash|
      "expected grouped amounts #{@actual.inspect} to match #{@expected.inspect}"
    end
  end
end