Sha256: 536790eba772fc3c690fd149804f49426bbdf7efe84eac1b5a84ca99595e49d4

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'rspec/expectations'

module MoneyRails
  module TestHelpers
    extend RSpec::Matchers::DSL

    matcher :monetize do |attr|

      chain(:with_currency) do |currency|
        @currency_iso = currency
      end

      chain(:as) do |virt_attr|
        @as = virt_attr
      end

      match do |target|
        matched = true
        money_attr = @as.presence || attr.to_s.sub(/_cents$/, "")
        matched = false if !target.respond_to?(money_attr) ||
          !target.send(money_attr).instance_of?(Money) ||
          (@currency_iso &&
           target.send(money_attr.to_sym).currency.id != @currency_iso)
        matched
      end

      description do
        description = "monetize #{attr}"
        description << " as #{@as}" if @as
        description << " with currency #{@currency_iso}" if @currency_iso
        description
      end

      failure_message_for_should do |actual|
        msg = "expected that #{attr} of #{actual} would be monetized"
        msg << " as #{@as}" if @as
        msg << " with currency #{@currency_iso}" if @currency_iso
        msg
      end

      failure_message_for_should_not do |actual|
        msg = "expected that #{attr} of #{actual} would not be monetized"
        msg << " as #{@as}" if @as
        msg << " with currency #{@currency_iso}" if @currency_iso
        msg
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
money-rails-0.11.0 lib/money-rails/test_helpers.rb
money-rails-0.10.0 lib/money-rails/test_helpers.rb
money-rails-0.9.0 lib/money-rails/test_helpers.rb