Module: Mocha::API
- Includes:
- Hooks, ParameterMatchers
- Included in:
- Integration::MiniTest::Adapter, Integration::TestUnit::Adapter
- Defined in:
- lib/mocha/api.rb
Overview
Methods added to Test::Unit::TestCase
, MiniTest::Unit::TestCase
or equivalent. The mock creation methods are #mock, #stub and #stub_everything, all of which return a #Mock which can be further modified by Mock#responds_like and Mock#responds_like_instance_of methods, both of which return a Mock, too, and can therefore, be chained to the original creation methods.
Mock#responds_like and Mock#responds_like_instance_of force the mock to indicate what it is supposed to be mocking, thus making it a safer verifying mock. They check that the underlying responder
will actually respond to the methods being stubbed, throwing a NoMethodError
upon invocation otherwise.
Instance Method Summary collapse
-
#mock(*arguments) ⇒ Mock
Builds a new mock object.
-
#sequence(name) ⇒ Sequence
Builds a new sequence which can be used to constrain the order in which expectations can occur.
-
#states(name) ⇒ StateMachine
Builds a new state machine which can be used to constrain the order in which expectations can occur.
-
#stub(*arguments) ⇒ Mock
Builds a new mock object.
-
#stub_everything(*arguments) ⇒ Mock
Builds a mock object that accepts calls to any method.
Methods included from Hooks
#mocha_setup, #mocha_teardown, #mocha_verify
Methods included from ParameterMatchers
#Not, #all_of, #any_of, #any_parameters, #anything, #equals, #equivalent_uri, #has_entries, #has_entry, #has_key, #has_keys, #has_value, #includes, #instance_of, #is_a, #kind_of, #optionally, #regexp_matches, #responds_with, #yaml_equivalent
Instance Method Details
#mock(name) ⇒ Mock #mock(expected_methods_vs_return_values = {}) ⇒ Mock #mock(name, expected_methods_vs_return_values = {}) ⇒ Mock
Builds a new mock object
69 70 71 72 73 74 75 |
# File 'lib/mocha/api.rb', line 69 def mock(*arguments) name = arguments.shift.to_s if arguments.first.is_a?(String) || arguments.first.is_a?(Symbol) expectations = arguments.shift || {} mock = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock mock.expects(expectations) mock end |
#sequence(name) ⇒ Sequence
Builds a new sequence which can be used to constrain the order in which expectations can occur.
Specify that an expected invocation must occur within a named Sequence by using Expectation#in_sequence.
159 160 161 |
# File 'lib/mocha/api.rb', line 159 def sequence(name) Sequence.new(name) end |
#states(name) ⇒ StateMachine
Builds a new state machine which can be used to constrain the order in which expectations can occur.
Specify the initial state of the state machine by using StateMachine#starts_as.
Specify that an expected invocation should change the state of the state machine by using Expectation#then.
Specify that an expected invocation should be constrained to occur within a particular state
by using Expectation#when.
A test can contain multiple state machines.
188 189 190 |
# File 'lib/mocha/api.rb', line 188 def states(name) Mockery.instance.new_state_machine(name) end |
#stub(name) ⇒ Mock #stub(stubbed_methods_vs_return_values = {}) ⇒ Mock #stub(name, stubbed_methods_vs_return_values = {}) ⇒ Mock
Builds a new mock object
96 97 98 99 100 101 102 |
# File 'lib/mocha/api.rb', line 96 def stub(*arguments) name = arguments.shift.to_s if arguments.first.is_a?(String) || arguments.first.is_a?(Symbol) expectations = arguments.shift || {} stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock stub.stubs(expectations) stub end |
#stub_everything(name) ⇒ Mock #stub_everything(stubbed_methods_vs_return_values = {}) ⇒ Mock #stub_everything(name, stubbed_methods_vs_return_values = {}) ⇒ Mock
Builds a mock object that accepts calls to any method. By default it will return nil
for any method call.
123 124 125 126 127 128 129 130 |
# File 'lib/mocha/api.rb', line 123 def stub_everything(*arguments) name = arguments.shift if arguments.first.is_a?(String) || arguments.first.is_a?(Symbol) expectations = arguments.shift || {} stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock stub.stub_everything stub.stubs(expectations) stub end |