Class: FlexMock
- Includes:
- Test::Unit::Assertions
- Defined in:
- lib/monetra/active_support/vendor/flexmock.rb
Overview
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.
Class Method Summary collapse
-
.use ⇒ Object
Class method to make sure that verify is called at the end of a test.
Instance Method Summary collapse
-
#initialize ⇒ FlexMock
constructor
Create a FlexMock object.
-
#method_missing(sym, *args, &block) ⇒ Object
Handle missing methods by attempting to look up a handler.
-
#mock_count(sym) ⇒ Object
Report how many times a method was called.
-
#mock_handle(sym, expected_count = nil, &block) ⇒ Object
Handle all messages denoted by
sym
by calling the given block and passing any parameters to the block. -
#mock_ignore_missing ⇒ Object
Ignore all undefined (missing) method calls.
-
#mock_verify ⇒ Object
Verify that each method that had an explicit expected count was actually called that many times.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Handle missing methods by attempting to look up a handler.
66 67 68 69 70 71 72 73 74 |
# File 'lib/monetra/active_support/vendor/flexmock.rb', line 66 def method_missing(sym, *args, &block) if handler = @handlers[sym] @counts[sym] += 1 args << block if block_given? handler.call(*args) else super(sym, *args, &block) unless @ignore_missing end end |
Class Method Details
.use ⇒ Object
Class method to make sure that verify is called at the end of a test.
78 79 80 81 82 83 |
# File 'lib/monetra/active_support/vendor/flexmock.rb', line 78 def self.use mock = new yield mock ensure mock.mock_verify end |
Instance Method Details
#mock_count(sym) ⇒ Object
Report how many times a method was called.
56 57 58 |
# File 'lib/monetra/active_support/vendor/flexmock.rb', line 56 def mock_count(sym) @counts[sym] end |
#mock_handle(sym, expected_count = nil, &block) ⇒ Object
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.
36 37 38 39 40 41 42 43 |
# File 'lib/monetra/active_support/vendor/flexmock.rb', line 36 def mock_handle(sym, expected_count=nil, &block) if block_given? @handlers[sym] = block else @handlers[sym] = proc { } end @expected_counts[sym] = expected_count if expected_count end |
#mock_ignore_missing ⇒ Object
Ignore all undefined (missing) method calls.
61 62 63 |
# File 'lib/monetra/active_support/vendor/flexmock.rb', line 61 def mock_ignore_missing @ignore_missing = true end |
#mock_verify ⇒ Object
Verify that each method that had an explicit expected count was actually called that many times.
47 48 49 50 51 52 53 |
# File 'lib/monetra/active_support/vendor/flexmock.rb', line 47 def mock_verify @expected_counts.keys.each do |key| assert_equal @expected_counts[key], @counts[key], "Expected method #{key} to be called #{@expected_counts[key]} times, " + "got #{@counts[key]}" end end |