Class: Crspec::Mock::StubChain

Inherits:
Object
  • Object
show all
Defined in:
lib/crspec/mock/double.rb

Direct Known Subclasses

ExpectationChain

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, method_name) ⇒ StubChain

Returns a new instance of StubChain.



32
33
34
35
36
37
38
# File 'lib/crspec/mock/double.rb', line 32

def initialize(target, method_name)
  @target = target
  @method_name = method_name.to_sym
  @expected_args = nil
  @return_proc = proc {}
  register_default_stub
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



30
31
32
# File 'lib/crspec/mock/double.rb', line 30

def method_name
  @method_name
end

#targetObject (readonly)

Returns the value of attribute target.



30
31
32
# File 'lib/crspec/mock/double.rb', line 30

def target
  @target
end

Instance Method Details

#and_raise(exception = StandardError, message = nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/crspec/mock/double.rb', line 62

def and_raise(exception = StandardError, message = nil)
  @return_proc = proc do
    err = exception.is_a?(Class) ? exception.new(message) : exception
    raise err
  end
  update_stub
  self
end

#and_return(*values) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/crspec/mock/double.rb', line 46

def and_return(*values)
  if values.size == 1
    val = values.first
    @return_proc = proc { val }
  else
    idx = 0
    @return_proc = proc do
      res = values[idx] || values.last
      idx += 1
      res
    end
  end
  update_stub
  self
end

#and_yield(*yield_args) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/crspec/mock/double.rb', line 71

def and_yield(*yield_args)
  old_proc = @return_proc
  @return_proc = proc do |*args, &block|
    block&.call(*yield_args)
    old_proc.call(*args)
  end
  update_stub
  self
end

#with(*args) ⇒ Object



40
41
42
43
44
# File 'lib/crspec/mock/double.rb', line 40

def with(*args)
  @expected_args = args
  update_stub
  self
end