Class: Quacky::Stub

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

Instance Method Summary collapse

Constructor Details

#initialize(method) ⇒ Stub

Returns a new instance of Stub.



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

def initialize method
  @method = method
end

Instance Method Details

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



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

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

#call(*args) ⇒ Object



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

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



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

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



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

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