Class: Facon::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-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.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/motion-facon/expectation.rb', line 16

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.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def actual_received_count
  @actual_received_count
end

#argument_expectationObject (readonly)

Returns the value of attribute argument_expectation.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def argument_expectation
  @argument_expectation
end

#error_generatorObject (readonly)

Returns the value of attribute error_generator.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def error_generator
  @error_generator
end

#expectation_orderingObject (readonly)

Returns the value of attribute expectation_ordering.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def expectation_ordering
  @expectation_ordering
end

#expected_fromObject (readonly)

Returns the value of attribute expected_from.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def expected_from
  @expected_from
end

#expected_received_countObject (readonly)

Returns the value of attribute expected_received_count.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def expected_received_count
  @expected_received_count
end

#methodObject (readonly)

Returns the value of attribute method.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

def method
  @method
end

#method_blockObject (readonly)

Returns the value of attribute method_block.



14
15
16
# File 'lib/motion-facon/expectation.rb', line 14

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).



46
47
48
# File 'lib/motion-facon/expectation.rb', line 46

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

#and_return(value) ⇒ Object

Sets up the expected method to return the given value.



32
33
34
35
36
# File 'lib/motion-facon/expectation.rb', line 32

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.



51
52
53
# File 'lib/motion-facon/expectation.rb', line 51

def and_throw(sym)
  @symbol_to_throw = sym
end

#and_yield(*args) ⇒ Object

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



39
40
41
42
# File 'lib/motion-facon/expectation.rb', line 39

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

#invoke(args, block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/motion-facon/expectation.rb', line 61

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.



96
97
98
# File 'lib/motion-facon/expectation.rb', line 96

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.



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

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)


87
88
89
90
91
92
# File 'lib/motion-facon/expectation.rb', line 87

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)


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

def negative_expectation_for?(method)
  false
end

#neverObject



117
# File 'lib/motion-facon/expectation.rb', line 117

def never; times(0); end

#onceObject



115
# File 'lib/motion-facon/expectation.rb', line 115

def once; times(1); end

#raise_block_failed_error(*args) ⇒ Object



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

def raise_block_failed_error(*args)
  @error_generator.raise_block_failed_error *args
end

#raise_expectation_error(*args) ⇒ Object



6
7
8
# File 'lib/motion-facon/expectation.rb', line 6

def raise_expectation_error(*args)
  @error_generator.raise_expectation_error *args
end

#times(val) ⇒ Object



110
111
112
113
# File 'lib/motion-facon/expectation.rb', line 110

def times(val)
  @expected_received_count = val
  self
end

#with(*args, &block) ⇒ Object



55
56
57
58
59
# File 'lib/motion-facon/expectation.rb', line 55

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