Class FlexMock
In: vendor/rails/activesupport/lib/active_support/vendor/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.

Usage: See TestSamples for example usage.

Methods

Included Modules

Test::Unit::Assertions

Public Class methods

Create a FlexMock object.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 25
25:   def initialize
26:     @handlers = Hash.new
27:     @counts   = Hash.new(0)
28:     @expected_counts = Hash.new
29:   end

Class method to make sure that verify is called at the end of a test.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 78
78:   def self.use
79:     mock = new
80:     yield mock
81:   ensure
82:     mock.mock_verify
83:   end

Public Instance methods

Handle missing methods by attempting to look up a handler.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 66
66:   def method_missing(sym, *args, &block)
67:     if handler = @handlers[sym]
68:       @counts[sym] += 1
69:       args << block  if block_given?
70:       handler.call(*args)
71:     else
72:       super(sym, *args, &block)  unless @ignore_missing
73:     end
74:   end

Report how many times a method was called.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 56
56:   def mock_count(sym)
57:     @counts[sym]
58:   end

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.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 36
36:   def mock_handle(sym, expected_count=nil, &block)
37:     if block_given?
38:       @handlers[sym] = block
39:     else
40:       @handlers[sym] = proc { }
41:     end
42:     @expected_counts[sym] = expected_count  if expected_count
43:   end

Ignore all undefined (missing) method calls.

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 61
61:   def mock_ignore_missing
62:     @ignore_missing = true
63:   end

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

[Source]

    # File vendor/rails/activesupport/lib/active_support/vendor/flexmock.rb, line 47
47:   def mock_verify
48:     @expected_counts.keys.each do |key|
49:       assert_equal @expected_counts[key], @counts[key],
50:         "Expected method #{key} to be called #{@expected_counts[key]} times, " +
51:         "got #{@counts[key]}"
52:     end
53:   end

[Validate]