Object
Deprecated Methods
The following methods are no longer supported in FlexMock. Include this file for legacy applications.
Permission is granted for use, copying, modification, distribution, and distribution of modified versions of this work as long as the above copyright notice is included.
+++
FlexMock is a flexible mock object framework for supporting testing.
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("name") m.should_receive(:upcase).with("stuff"). and_return("STUFF") m.should_receive(:downcase).with(String). and_return { |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.
Permission is granted for use, copying, modification, distribution, and distribution of modified versions of this work as long as the above copyright notice is included.
+++
Create a FlexMock object with the given name. The name is used in error messages. If no container is given, create a new, one-off container for this mock.
# File lib/flexmock/core.rb, line 55 def initialize(name="unknown", container=nil) @flexmock_name = name @expectations = Hash.new @ignore_missing = false @verified = false container = UseContainer.new if container.nil? container.flexmock_remember(self) end
Undefined is normally available as FlexMock.undefined
# File lib/flexmock/undefined.rb, line 47 def self.undefined @undefined end
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 mocks that will be automatically verified in the test teardown by using the flexmock method.
# File lib/flexmock/core_class_methods.rb, line 39 def use(*names) names = ["unknown"] if names.empty? container = UseContainer.new mocks = names.collect { |n| container.flexmock(n) } yield(*mocks) rescue Exception => _ container.got_exception = true raise ensure container.flexmock_teardown end
# File lib/flexmock/core.rb, line 91 def by_default @last_expectation.by_default self end
Save the original definition of respond_to? for use a bit later.
Teardown and infrastructure setup for this mock.
# File lib/flexmock/core.rb, line 82 def flexmock_teardown end
Verify that each method that had an explicit expected count was actually called that many times.
# File lib/flexmock/core.rb, line 71 def flexmock_verify return if @verified @verified = true flexmock_wrap do @expectations.each do |sym, handler| handler.flexmock_verify end end end
Return the inspection string for a mock.
# File lib/flexmock/core.rb, line 65 def inspect "<FlexMock:#{flexmock_name}>" end
Override the built-in method to include the mocked methods.
# File lib/flexmock/core.rb, line 130 def method(sym) @expectations[sym] || super rescue NameError => ex if @ignore_missing proc { FlexMock.undefined } else raise ex end end
Handle missing methods by attempting to look up a handler.
# File lib/flexmock/core.rb, line 97 def method_missing(sym, *args, &block) flexmock_wrap do if handler = @expectations[sym] args << block if block_given? handler.call(*args) elsif @ignore_missing FlexMock.undefined else super(sym, *args, &block) end end end
Override the built-in respond_to? to include the mocked methods.
# File lib/flexmock/core.rb, line 114 def respond_to?(sym, *args) super || (@expectations[sym] ? true : @ignore_missing) end
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
# File lib/flexmock/core.rb, line 182 def should_expect yield Recorder.new(self) end
Ignore all undefined (missing) method calls.
# File lib/flexmock/core.rb, line 86 def should_ignore_missing @ignore_missing = true end
Declare that the mock object should receive a message with the given name.
If more than one method name is given, then the mock object should expect to receive all the listed melthods. If a hash of method name/value pairs is given, then the each method will return the associated result. Any expectations applied to the result of should_receive will be applied to all the methods defined in the argument list.
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.
# File lib/flexmock/core.rb, line 159 def should_receive(*args) @last_expectation = ContainerHelper.parse_should_args(self, args) do |sym| @expectations[sym] ||= ExpectationDirector.new(sym) result = Expectation.new(self, sym) @expectations[sym] << result override_existing_method(sym) if flexmock_respond_to?(sym) result end @last_expectation end
Generated with the Darkfish Rdoc Generator 2.