Class: Substitute

Inherits:
Object show all
Defined in:
lib/rsubstitute.rb

Instance Method Summary collapse

Constructor Details

#initializeSubstitute

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

Instance Method Details

#receivedObject



33
34
35
36
# File 'lib/rsubstitute.rb', line 33

def received
    @assert_next = true
    self
end

#returns(value) ⇒ Object



59
60
61
# File 'lib/rsubstitute.rb', line 59

def returns(value)
    @results << [@calls.pop(), value]
end