Module: Financial::RSpecMatchers

Defined in:
lib/financial/rspec_matchers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/financial/rspec_matchers.rb', line 3

def self.included(base)
  create_include_balance_matcher
end

Instance Method Details

#create_include_balance_matcherObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/financial/rspec_matchers.rb', line 7

def create_include_balance_matcher
  RSpec::Matchers.define :include_balance do |expected|
    chain :in_date do |date|
      @date = Financial::FinancialDate.new(date).date
    end

    failure_message_for_should do |actual|
      if @date
        "expected #{actual} to have balance #{expected} in date #{@date}"
      else
        "expected #{actual} to have balance #{expected}"
      end
    end

    match do |actual|
      actual.should_not be_empty
      if @date
        actual.any? { |balance| balance.date == @date and balance.value == expected }
      else
        actual.any? { |balance| balance.value == expected }
      end
    end
  end
end