Class: Substitute
Instance Method Summary collapse
-
#initialize ⇒ Substitute
constructor
A new instance of Substitute.
- #method_missing(method, *args, &block) ⇒ Object
- #received ⇒ Object
- #returns(value) ⇒ Object
Constructor Details
#initialize ⇒ Substitute
Returns a new instance of Substitute.
27 28 29 30 31 |
# File 'lib/rsubstitute.rb', line 27 def initialize() @calls = [] @results = [] @assert_next = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rsubstitute.rb', line 38 def method_missing(method, *args, &block) assert_was_received = @assert_next @assert_next = false if assert_was_received and not @calls.any? {|x| x.matches(method, args)} raise "Call #{method} not received with specified args" end @calls << Call.new(method, args) matching_result_pair = @results.reverse.find do |x| x[0].matches(method, args) end if (matching_result_pair.nil?) value_to_return = Object.new else value_to_return = matching_result_pair[1] end value_to_return.instance_variable_set(:@substitute, self) return value_to_return end |