Class: Gimme::Verifies

Inherits:
BlankSlate show all
Defined in:
lib/gimme/verifies.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(double, times = 1) ⇒ Verifies

Returns a new instance of Verifies.



5
6
7
8
9
# File 'lib/gimme/verifies.rb', line 5

def initialize(double,times=1)
  @double = double
  @times = times
  @raises_no_method_error = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gimme/verifies.rb', line 11

def method_missing(sym, *args, &block)
  sym = ResolvesMethods.new(@double.cls,sym,args).resolve(@raises_no_method_error)

  #gosh, this loop sure looks familiar. just like another ugly loop I know. TODO.
  invoked = 0
  if @double.invocations[sym]
     @double.invocations[sym].each do |invoke_args,count|
       matching = args.size == invoke_args.size
       invoke_args.each_index do |i|
         unless invoke_args[i] == args[i]  || (args[i].respond_to?(:matches?) && args[i].matches?(invoke_args[i]))
           matching = false
           break
         end
       end
       invoked += count if matching
     end
  end

  if invoked != @times
    msg = "expected #{@double.cls.to_s}##{sym} to have been called with arguments #{args}"
    if !@double.invocations[sym] || @double.invocations[sym].empty?
      msg << "\n  but was never called"
    else
      msg = @double.invocations[sym].inject msg do |memo, actual|
        memo + "\n  was actually called #{actual[1]} times with arguments #{actual[0]}"
      end
    end

    raise Errors::VerificationFailedError.new(msg)
  end
end

Instance Attribute Details

#raises_no_method_errorObject

Returns the value of attribute raises_no_method_error.



4
5
6
# File 'lib/gimme/verifies.rb', line 4

def raises_no_method_error
  @raises_no_method_error
end