Module: Stendhal::Mocks::Mockable

Included in:
Object
Defined in:
lib/stendhal/mocks/mockable.rb

Instance Method Summary collapse

Instance Method Details

#and_returns(retval = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stendhal/mocks/mockable.rb', line 48

def and_returns(retval = nil, &block)
  raise "This object has no mocks." unless @__verifier
  method = __verifier.last_mocked_method
  unless respond_to?(:"__unstubbed_#{method}")
    metaclass = (class << self;self;end)
    metaclass.send(:alias_method, :"__unstubbed_#{method}", :"__original_#{method}")
    metaclass.send(:undef_method, :"__original_#{method}")

    return_value = block || Proc.new { retval }
    metaclass.send(:define_method, :"__original_#{method}", return_value)
  end
end

#does_not_expect(method) ⇒ Object



61
62
63
# File 'lib/stendhal/mocks/mockable.rb', line 61

def does_not_expect(method)
  expects(method, :negative => true)
end

#exactly(times) ⇒ Object



37
38
39
40
41
# File 'lib/stendhal/mocks/mockable.rb', line 37

def exactly(times)
  raise "This object has no mocks." unless @__verifier
  __verifier.expectation_for(__verifier.last_mocked_method).times_expected = times
  self
end

#expects(method, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/stendhal/mocks/mockable.rb', line 5

def expects(method, options = {})
  if options[:negative]
    __verifier.add_negative_expectation(method) 
  else
    __verifier.add_expectation(method) 
  end
  unless respond_to?(:"__original_#{method}")
    metaclass = (class << self;self;end)
    metaclass.send(:alias_method, :"__original_#{method}", method.to_sym)
    metaclass.send(:undef_method, method.to_sym)
    metaclass.class_eval <<EOT
      def #{method}(*args, &block)
        @__verifier.fulfill_expectation(:#{method},*args,&block)
        __original_#{method}(*args,&block)
      end
EOT
  end
  self
end

#onceObject



25
26
27
28
29
# File 'lib/stendhal/mocks/mockable.rb', line 25

def once
  raise "This object has no mocks." unless @__verifier
  __verifier.expectation_for(__verifier.last_mocked_method).times_expected = 1
  self
end

#timesObject



43
44
45
46
# File 'lib/stendhal/mocks/mockable.rb', line 43

def times
  raise "This object has no mocks." unless @__verifier
  self
end

#twiceObject



31
32
33
34
35
# File 'lib/stendhal/mocks/mockable.rb', line 31

def twice
  raise "This object has no mocks." unless @__verifier
  __verifier.expectation_for(__verifier.last_mocked_method).times_expected = 2
  self
end