Class: Fakes::MethodStub

Inherits:
Object
  • Object
show all
Defined in:
lib/fakes/method_stub.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg_sets = []) ⇒ MethodStub

Returns a new instance of MethodStub.



3
4
5
# File 'lib/fakes/method_stub.rb', line 3

def initialize(arg_sets = [])
  @arg_sets = arg_sets
end

Instance Method Details

#add_new_argument_set(set) ⇒ Object



7
8
9
10
# File 'lib/fakes/method_stub.rb', line 7

def add_new_argument_set(set)
  @arg_sets << set
  set
end

#and_return(item) ⇒ Object



24
25
26
# File 'lib/fakes/method_stub.rb', line 24

def and_return(item)
  ignore_arg.and_return(item)
end

#called_with(*args) ⇒ Object



34
35
36
# File 'lib/fakes/method_stub.rb', line 34

def called_with(*args)
  return @arg_sets.find{|item| item.was_called_with?(args)}
end

#ignore_argObject



20
21
22
# File 'lib/fakes/method_stub.rb', line 20

def ignore_arg
  return add_new_argument_set(IgnoreSet.new)
end

#invoke(args) ⇒ Object



28
29
30
31
32
# File 'lib/fakes/method_stub.rb', line 28

def invoke(args)
  set = @arg_sets.find{|item| item.matches?(args)} || ignore_arg
  set.capture_args(args)
  return set.process
end

#throws(exception) ⇒ Object



16
17
18
# File 'lib/fakes/method_stub.rb', line 16

def throws(exception)
  ignore_arg.throws(exception)
end

#times?(value) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/fakes/method_stub.rb', line 42

def times?(value)
  return total_times_called == value
end

#total_times_calledObject



38
39
40
# File 'lib/fakes/method_stub.rb', line 38

def total_times_called
  return @arg_sets.inject(0){|sum,item|sum += item.times_called}
end

#with(*args) ⇒ Object



12
13
14
# File 'lib/fakes/method_stub.rb', line 12

def with(*args)
  return add_new_argument_set(ArgSet.new(args))
end