Class: Quacky::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/quacky/stub.rb

Instance Method Summary collapse

Constructor Details

#initialize(method, &block) ⇒ Stub

Returns a new instance of Stub.



7
8
9
10
# File 'lib/quacky/stub.rb', line 7

def initialize method, &block
  @method = method
  @return_block = block
end

Instance Method Details

#and_return(value = nil, &block) ⇒ Object



18
19
20
21
22
# File 'lib/quacky/stub.rb', line 18

def and_return value=nil, &block
  @return_value = value
  @return_block = block
  self
end

#call(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/quacky/stub.rb', line 24

def call *args
  @called_args = args
  validate_expectation
  call_through *args

  if expected_args
    return_value if (called_args == expected_args)
  else
    return_value
  end
end

#validate_satisfaction!Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/quacky/stub.rb', line 36

def validate_satisfaction!
  if expected_args
    if called_args == expected_args
      true
    else
      raise UnsatisfiedExpectation
    end
  else
    was_called? || raise(UnsatisfiedExpectation, "Expected `#{@method.name}` to be called.")
  end
end

#with(*args) ⇒ Object



12
13
14
15
16
# File 'lib/quacky/stub.rb', line 12

def with *args
  @expected_args = args
  call_through *args
  self
end