| Class | FlexMock |
| In: |
lib/flexmock.rb
|
| Parent: | Object |
FlexMock is a flexible mock object suitable for using with Ruby’s Test::Unit unit test framework. FlexMock has a simple interface that’s easy to remember, and leaves the hard stuff to all those other mock object implementations.
Basic Usage:
m = FlexMock.new("name")
m.mock_handle(:meth) { |args| assert_stuff }
Simplified Usage:
m = FlexMock.new("name")
m.should_receive(:upcase).with("stuff").
returns("STUFF")
m.should_receive(:downcase).with(String).
returns { |s| s.downcase }.once
With Test::Unit Integration:
class TestSomething < Test::Unit::TestCase
include FlexMock::TestCase
def test_something
m = flexmock("name")
m.should_receive(:hi).and_return("Hello")
m.hi
end
end
Note: When using Test::Unit integeration, don’t forget to include FlexMock::TestCase. Also, if you override teardown, make sure you call super.
| ANY | = | AnyMatcher.new |
| mock_current_order | [RW] | |
| mock_groups | [R] | |
| mock_name | [R] |
Check will assert the block returns true. If it doesn’t, an assertion failure is triggered with the given message.
Create a FlexMock object with the given name. The name is used in error messages.
Class method to make sure that verify is called at the end of a test. One mock object will be created for each name given to the use method. The mocks will be passed to the block as arguments. If no names are given, then a single anonymous mock object will be created.
At the end of the use block, each mock object will be verified to make sure the proper number of calls have been made.
Usage:
FlexMock.use("name") do |mock| # Creates a mock named "name"
mock.should_receive(:meth).
returns(0).once
end # mock is verified here
NOTE: If you include FlexMock::TestCase into your test case file, you can create moks that will be automatically verified in the test teardown by using the flexmock method.
Handle all messages denoted by sym by calling the given block and passing any parameters to the block. If we know exactly how many calls are to be made to a particular method, we may check that by passing in the number of expected calls as a second paramter.
Declare that the mock object should expect methods by providing a recorder for the methods and having the user invoke the expected methods in a block. Further expectations may be applied the result of the recording call.
Example Usage:
mock.should_expect do |record|
record.add(Integer, 4) { |a, b|
a + b
}.at_least.once
Declare that the mock object should receive a message with the given name. An expectation object for the method name is returned as the result of this method. Further expectation constraints can be added by chaining to the result.
See Expectation for a list of declarators that can be used.