Class: Mockr::Mock
Instance Attribute Summary collapse
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
Instance Method Summary collapse
-
#expects ⇒ Object
Tell the mock to expect a call, optionally with specific parameters.
-
#initialize(&block) ⇒ Mock
constructor
Create a new Mock, with an optional initialisation block.
-
#stubs ⇒ Object
Tell the mock to respond to a call, optionally with specific parameters.
-
#use(&block) ⇒ Object
Execute the given block and call #verify afterwards.
-
#verify ⇒ Object
Check that the expected calls to this mock were made, and raise a Test::Unit::AssertionFailed exception otherwise.
Constructor Details
#initialize(&block) ⇒ Mock
Create a new Mock, with an optional initialisation block. If provided, the block will be called with the new instance.
Example:
Mock.new do |m|
m.stubs.some_method
end
19 20 21 22 23 24 |
# File 'lib/mockr.rb', line 19 def initialize &block @expectations = [] @satisfied_expectations = [] create_proxy block.call(self) if block end |
Instance Attribute Details
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
10 11 12 |
# File 'lib/mockr.rb', line 10 def proxy @proxy end |
Instance Method Details
#expects ⇒ Object
Tell the mock to expect a call, optionally with specific parameters. If the call has not been made when #verify is called, #verify will fail with a Test::Unit::AssertionFailed.
Parameters to the expected call will be used to match the actual parameters passed by client code later. The match (===) method of the expectation parameter is used to determine whether the client’s call matched this anticipated call.
Examples:
mock.expects.bang!
mock.expects.ping.as { :pong }
mock.expects.safe?(1..10).as { true } # Expect a call with a parameter for which <tt>(1..10) === param</tt>
57 58 59 |
# File 'lib/mockr.rb', line 57 def expects CallRecorder.new(method(:expect_call)) end |
#stubs ⇒ Object
Tell the mock to respond to a call, optionally with specific parameters.
The call can be called an arbitrary number of times by the client code without affecting the result of #verify.
Parameters to the expected call will be used to match the actual parameters passed by client code later. The match (===) method of the expectation parameter is used to determine whether the client’s call matched this anticipated call.
Examples:
mock.stubs.bang!
mock.stubs.ping.as { :pong }
mock.stubs.hello?(/World/).as { true } # Respond with +true+ if called with a parameter for which <tt>/World/ === param</tt> is true
40 41 42 |
# File 'lib/mockr.rb', line 40 def stubs CallRecorder.new(method(:stub_call)) end |
#use(&block) ⇒ Object
Execute the given block and call #verify afterwards. You are unlikely to use this method, since the methods in TestCaseHelpers render it somewhat redundant.
75 76 77 78 |
# File 'lib/mockr.rb', line 75 def use &block block.call(proxy) verify end |
#verify ⇒ Object
Check that the expected calls to this mock were made, and raise a Test::Unit::AssertionFailed exception otherwise. This method will be called automatically if you use the methods provided by TestCaseHelpers
65 66 67 68 69 70 |
# File 'lib/mockr.rb', line 65 def verify missing_expectations = @expectations - @satisfied_expectations if missing_expectations.any? raise AssertionFailedError.new("Expected #{missing_expectations[0]} did not happen") end end |