Module: Synthesis::ExpectationInterceptor

Defined in:
lib/synthesis/expectation_interceptor.rb

Overview

Extend by the mock object framework’s expectation mechanism to allow Synthesis to tap into it in order to collect simulated method arguments and return values.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(receiver) ⇒ Object

Classes extending ExpectationInterceptor will have a synthesis_expectation attribute accessor added to them.



96
97
98
# File 'lib/synthesis/expectation_interceptor.rb', line 96

def self.extended(receiver)
  receiver.send(:attr_accessor, :synthesis_expectation)
end

Instance Method Details

#intercept_expected_arguments_on(method_name) ⇒ Object

Intercept the mock object framework’s expectation method for declaring a mocked method’s arguments so that Synthesis can record them.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/synthesis/expectation_interceptor.rb', line 28

def intercept_expected_arguments_on(method_name)
  (@original_methods ||= []) << method_name
  
  class_eval do
    alias_method "intercepted_#{method_name}", method_name
    
    define_method(:get_method_name) {method_name}
    
    def temp_method(*expected_parameters, &matching_block)
      synthesis_expectation.args = expected_parameters if synthesis_expectation
      send("intercepted_#{get_method_name}", *expected_parameters, &matching_block)
    end
    
    alias_method method_name, :temp_method
    undef temp_method
  end
end

#intercept_expected_return_values_on(method_name) ⇒ Object

Intercept the mock object framework’s expectation method for declaring a mocked method’s return values so that Synthesis can record them.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/synthesis/expectation_interceptor.rb', line 48

def intercept_expected_return_values_on(method_name)
  (@original_methods ||= []) << method_name
  
  class_eval do
    alias_method "intercepted_#{method_name}", method_name
    
    define_method(method_name) do |*values|
      mock_expectation = send("intercepted_#{method_name}", *values)
      synthesis_expectation.add_return_values(*values) if synthesis_expectation
      mock_expectation
    end
  end
end

#intercept_test_subject_on(method_name) ⇒ Object

Intercept the actual mock proxy to record the test subject so that Synthesis can track which object is being tested



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/synthesis/expectation_interceptor.rb', line 8

def intercept_test_subject_on(method_name)
  (@original_methods ||= []) << method_name
  
  class_eval do
    alias_method "intercepted_#{method_name}", method_name
    
    define_method(:get_invoke_method_name) {method_name}
    
    def temp_invoke(*expected_parameters, &matching_block)
      synthesis_expectation.add_test_subject(caller(2)) if synthesis_expectation
      send("intercepted_#{get_invoke_method_name}", *expected_parameters, &matching_block)
    end
      
    alias_method method_name, :temp_invoke
    undef temp_invoke
  end
end

#remove_expectation_on(method_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/synthesis/expectation_interceptor.rb', line 62

def remove_expectation_on(method_name)
  (@original_methods ||= []) << method_name
  
  class_eval do
    alias_method "intercepted_#{method_name}", method_name
    
    define_method(method_name) do |*values|
      Synthesis::ExpectationRecord.remove(synthesis_expectation) if synthesis_expectation
      send("intercepted_#{method_name}")
    end
  end
end

#stop_intercepting!Object

Restore the original methods ExpectationInterceptor has rewritten and undefine their intercepted counterparts. Undefine the synthesis_expectation accessors.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/synthesis/expectation_interceptor.rb', line 78

def stop_intercepting!
  @original_methods.each do |m|
    class_eval do
      alias_method m, "intercepted_#{m}"
      remove_method "intercepted_#{m}"
    end
  end

  class_eval do
    remove_method :synthesis_expectation
    remove_method :synthesis_expectation=
    remove_method :get_invoke_method_name
    remove_method :get_method_name
  end
end