Class: Facon::Expectation

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/facon/expectation.rb

Overview

An Expectation, also know as a mock method, is an expectation that an object should receive a specific message during the execution of an example.

Direct Known Subclasses

NegativeExpectation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_generator, expectation_ordering, expected_from, method, method_block, expected_received_count = 1) ⇒ Expectation

Returns a new instance of Expectation.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/facon/expectation.rb', line 12

def initialize(error_generator, expectation_ordering, expected_from, method, method_block, expected_received_count = 1)
  @error_generator = error_generator
  @expectation_ordering = expectation_ordering
  @expected_from = expected_from
  @method = method
  @method_block = method_block
  @expected_received_count = expected_received_count

  @argument_expectation = :any
  @exception_to_raise = nil
  @symbol_to_throw = nil
  @actual_received_count = 0
  @args_to_yield = []
end

Instance Attribute Details

#actual_received_countObject (readonly)

Returns the value of attribute actual_received_count.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def actual_received_count
  @actual_received_count
end

#argument_expectationObject (readonly)

Returns the value of attribute argument_expectation.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def argument_expectation
  @argument_expectation
end

#error_generatorObject (readonly)

Returns the value of attribute error_generator.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def error_generator
  @error_generator
end

#expectation_orderingObject (readonly)

Returns the value of attribute expectation_ordering.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def expectation_ordering
  @expectation_ordering
end

#expected_fromObject (readonly)

Returns the value of attribute expected_from.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def expected_from
  @expected_from
end

#expected_received_countObject (readonly)

Returns the value of attribute expected_received_count.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def expected_received_count
  @expected_received_count
end

#methodObject (readonly)

Returns the value of attribute method.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def method
  @method
end

#method_blockObject (readonly)

Returns the value of attribute method_block.



10
11
12
# File 'lib/facon/expectation.rb', line 10

def method_block
  @method_block
end

Instance Method Details

#and_raise(exception = Exception) ⇒ Object

Sets up the expected method to raise the given exception (default: Exception).



42
43
44
# File 'lib/facon/expectation.rb', line 42

def and_raise(exception = Exception)
  @exception_to_raise = exception
end

#and_return(value) ⇒ Object

Sets up the expected method to return the given value.



28
29
30
31
32
# File 'lib/facon/expectation.rb', line 28

def and_return(value)
  raise MockExpectationError, 'Ambiguous return expectation' unless @method_block.nil?

  @return_block = proc { value }
end

#and_throw(sym) ⇒ Object

Sets up the expected method to throw the given symbol.



47
48
49
# File 'lib/facon/expectation.rb', line 47

def and_throw(sym)
  @symbol_to_throw = sym
end

#and_yield(*args) ⇒ Object

Sets up the expected method to yield with the given arguments.



35
36
37
38
# File 'lib/facon/expectation.rb', line 35

def and_yield(*args)
  @args_to_yield << args
  self
end

#invoke(args, block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/facon/expectation.rb', line 57

def invoke(args, block)
  begin
    raise @exception_to_raise unless @exception_to_raise.nil?
    throw @symbol_to_throw unless @symbol_to_throw.nil?

    return_value = if !@method_block.nil?
      invoke_method_block(args)
    elsif @args_to_yield.size > 0
      @args_to_yield.each { |curr_args| block.call(*curr_args) }
    else
      nil
    end

    if defined?(@return_block) && @return_block
      args << block unless block.nil?
      @return_block.call(*args)
    else
      return_value
    end
  ensure
    @actual_received_count += 1
  end
end

#matches(method, args) ⇒ Object

Returns true if the given method and arguments match this Expectation.



92
93
94
# File 'lib/facon/expectation.rb', line 92

def matches(method, args)
  @method == method && check_arguments(args)
end

#matches_name_but_not_args(method, args) ⇒ Object

Returns true if the given method matches this Expectation, but the given arguments don’t.



98
99
100
# File 'lib/facon/expectation.rb', line 98

def matches_name_but_not_args(method, args)
  @method == method && !check_arguments(args)
end

#met?Boolean

Returns true if this expectation has been met. TODO at_least and at_most conditions

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/facon/expectation.rb', line 83

def met?
  return true if @expected_received_count == :any ||
    @expected_received_count == @actual_received_count

  raise_expectation_error(self)
end

#negative_expectation_for?(method) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/facon/expectation.rb', line 102

def negative_expectation_for?(method)
  false
end

#neverObject



113
# File 'lib/facon/expectation.rb', line 113

def never; times(0); end

#onceObject



111
# File 'lib/facon/expectation.rb', line 111

def once; times(1); end

#times(val) ⇒ Object



106
107
108
109
# File 'lib/facon/expectation.rb', line 106

def times(val)
  @expected_received_count = val
  self
end

#with(*args, &block) ⇒ Object



51
52
53
54
55
# File 'lib/facon/expectation.rb', line 51

def with(*args, &block)
  @method_block = block if block
  @argument_expectation = args
  self
end