Parent

Included Modules

FlexMock

 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.

+++


 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.

+++


Deprecated Methods

The following methods are no longer supported in FlexMock. Include

this file for legacy applications.


                                                                    

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.

Constants

ContainerHelper
ANY

Attributes

framework_adapter[R]
flexmock_name[R]
flexmock_container[RW]

Public Class Methods

new(name="unknown", container=nil) click to toggle source

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
55:   def initialize(name="unknown", container=nil)
56:     @flexmock_name = name
57:     @expectations = Hash.new
58:     @ignore_missing = false
59:     @verified = false
60:     container = UseContainer.new if container.nil?
61:     container.flexmock_remember(self)
62:   end
undefined() click to toggle source

Undefined is normally available as FlexMock.undefined

    # File lib/flexmock/undefined.rb, line 43
43:   def self.undefined
44:     @undefined
45:   end
use(*names) click to toggle source

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
39:     def use(*names)
40:       names = ["unknown"] if names.empty?
41:       container = UseContainer.new
42:       mocks = names.collect { |n| container.flexmock(n) }
43:       yield(*mocks)
44:     rescue Exception => ex
45:       container.got_exception = true
46:       raise
47:     ensure
48:       container.flexmock_teardown
49:     end

Public Instance Methods

by_default() click to toggle source
    # File lib/flexmock/core.rb, line 91
91:   def by_default
92:     @last_expectation.by_default
93:     self
94:   end
flexmock_respond_to?(sym, *args) click to toggle source

Save the original definition of respond_to? for use a bit later.

Alias for: respond_to?
flexmock_teardown() click to toggle source

Teardown and infrastructure setup for this mock.

    # File lib/flexmock/core.rb, line 82
82:   def flexmock_teardown
83:   end
flexmock_verify() click to toggle source

Verify that each method that had an explicit expected count was actually called that many times.

    # File lib/flexmock/core.rb, line 71
71:   def flexmock_verify
72:     return if @verified
73:     @verified = true
74:     flexmock_wrap do
75:       @expectations.each do |sym, handler|
76:         handler.flexmock_verify
77:       end
78:     end
79:   end
inspect() click to toggle source

Return the inspection string for a mock.

    # File lib/flexmock/core.rb, line 65
65:   def inspect
66:     "<FlexMock:#{flexmock_name}>"
67:   end
method(sym) click to toggle source

Override the built-in method to include the mocked methods.

     # File lib/flexmock/core.rb, line 130
130:   def method(sym)
131:     @expectations[sym] || super
132:   rescue NameError => ex
133:     if @ignore_missing
134:       proc { FlexMock.undefined }
135:     else
136:       raise ex
137:     end
138:   end
method_missing(sym, *args, &block) click to toggle source

Handle missing methods by attempting to look up a handler.

     # File lib/flexmock/core.rb, line 97
 97:   def method_missing(sym, *args, &block)
 98:     flexmock_wrap do
 99:       if handler = @expectations[sym]
100:         args << block  if block_given?
101:         handler.call(*args)
102:       elsif @ignore_missing
103:         FlexMock.undefined
104:       else
105:         super(sym, *args, &block)
106:       end
107:     end
108:   end
mock_ignore_missing() click to toggle source
respond_to?(sym, *args) click to toggle source

Override the built-in respond_to? to include the mocked methods.

     # File lib/flexmock/core.rb, line 114
114:   def respond_to?(sym, *args)
115:     super || (@expectations[sym] ? true : @ignore_missing)
116:   end
Also aliased as: flexmock_respond_to?
should_expect() click to toggle source

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
182:   def should_expect
183:     yield Recorder.new(self)
184:   end
should_ignore_missing() click to toggle source

Ignore all undefined (missing) method calls.

    # File lib/flexmock/core.rb, line 86
86:   def should_ignore_missing
87:     @ignore_missing = true
88:   end
Also aliased as: mock_ignore_missing
) click to toggle source

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
159:   def should_receive(*args)
160:     @last_expectation = ContainerHelper.parse_should_args(self, args) do |sym|
161:       @expectations[sym] ||= ExpectationDirector.new(sym)
162:       result = Expectation.new(self, sym)
163:       @expectations[sym] << result
164:       override_existing_method(sym) if flexmock_respond_to?(sym)
165:       result
166:     end
167:     @last_expectation
168:   end

Private Instance Methods

flexmock_wrap(&block) click to toggle source

Wrap a block of code so the any assertion errors are wrapped so that the mock name is added to the error message .

     # File lib/flexmock/core.rb, line 190
190:   def flexmock_wrap(&block)
191:     yield
192:   rescue FlexMock.framework_adapter.assertion_failed_error => ex
193:     raise FlexMock.framework_adapter.assertion_failed_error,
194:     "in mock '#{@flexmock_name}': #{ex.message}",
195:     ex.backtrace
196:   end
override_existing_method(sym) click to toggle source

Override the existing definition of method sym in the mock. Most methods depend on the method_missing trick to be invoked. However, if the method already exists, it will not call method_missing. This method defines a singleton method on the mock to explicitly invoke the method_missing logic.

     # File lib/flexmock/core.rb, line 204
204:   def override_existing_method(sym)
205:     sclass.class_eval       def #{sym}(*args, &block)        method_missing(:#{sym}, *args, &block)      end
206:   end
sclass() click to toggle source

Return the singleton class of the mock object.

     # File lib/flexmock/core.rb, line 213
213:   def sclass
214:     class << self; self; end
215:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.