MockRepository

The MockRepository is the main interaction point with Rhino Mocks.

Common usage pattern is to create the <Rhino.Mocks.MockRepository> on [SetUp] and then create mock objects using either <Rhino.Mocks.MockRepository.CreateMock> or <Rhino.Mocks.MockRepository.DynamicMock> and setup expectations on the mock object(s) by callling their methods.  A call to <Rhino.Mocks.MockRepository.ReplayAll> would move the mock object(s) to replay state, a call to <Rhino.Mocks.MockRepository.VerifyAll> is made from the [TearDown] method.

Thread Safety

MockRepository is capable of verifying in multiply threads, but recording in multiply threads is not recommended.  If you need to do so you must use the <Rhino.Mocks.Expect.On> and <Rhino.Mocks.LastCall.On> methods and not <Rhino.Mocks.Expect.Call> and LastCall’s various methods.

Code Sample

MockRepository mocks;

[SetUp]
public void Setup()
{
mocks = new MockRepository();
}

[Test]
public void CallMethodOnObject()
{

IDemo demo = (IDemo)mocks.CreateMock(typeof(IDemo));
// Setting up an expectation for a call to IDemo.VoidNoArg
demo.VoidNoArg();
mocks.ReplayAll();
// Fullifying the expectation for call to IDemo.VoidNoArg
demo.VoidNoArg();
}

[TearDown]
public void Teardown()
{
mocks.VerifyAll();
}

Class Responsbilities

  • Create and manage mock object throughout their life time

See Also

  • <Rhino.Mocks.MockRepository.CreateMock>
  • <Rhino.Mocks.MockRepository.DynamicMock>
  • <Rhino.Mocks.MockRepository.ReplayAll>
  • <Rhino.Mocks.MockRepository.VerifyAll>
Summary
The MockRepository is the main interaction point with Rhino Mocks.
A static variable that is used to hold the repository that last had a method call on one of its mock objects.
The last proxy that had a method call for this repository
Gets the current recorder for the repository.
Create a new instance of MockRepository
Moves the entire MockRepository to use ordered recording.
Moves the entire MockRepository to use unordered recording (the default).
Create a mock object with strict semantics.
Create a mock object with dynamic semantics.
Create a mock object with from a class that defaults to calling the class methods if no expectation is set on the method.
Moves a single mock object to the replay state.
Moves the mocked object back to record state.
Verifies that all expectations has been met for a single mock object.
Gets the method options for the last call on mockedInstance
Handles a method call for a mock object.
Moves all the mock objects in the repository to replay state.
Verifies that all the expectations on all mock objects in the repository are met.
The replayer for the repository.
The last mock object that was called on any repository.

Variables

lastRepository

A static variable that is used to hold the repository that last had a method call on one of its mock objects.

lastProxy

The last proxy that had a method call for this repository

Properties

Recorder

Gets the current recorder for the repository.

Functions

MockRepository

Create a new instance of MockRepository

Ordered

Moves the entire MockRepository to use ordered recording.

This call is only valid during the recording phase.  This call affects all mock objects that were created from this repository.

The orderring is ended when the returned IDisposable’s Dispose() method is called.

[Test]
public void CallMethodOnObject()
{
IDemo demo = (IDemo)mocks.CreateMock(typeof(IDemo));
//Moving to ordered mocking.
using(mocks.Ordered()
{
demo.VoidNoArg();
demo.IntNoArg();
}
//Must exit the ordering before calling
mocks.ReplayAll();
//If we would try to call them in any other order, the test would fail
demo.VoidNoArg();
demo.IntNoArg();
}

Unordered

Moves the entire MockRepository to use unordered recording (the default).

This call is only valid during the recording phase.  This call affects all mock objects that were created from this repository.

[Test]
public void CallMethodOnObject()
{
IDemo demo = (IDemo)mocks.CreateMock(typeof(IDemo));
//Moving to ordered mocking.
using(mocks.Ordered()
{
demo.VoidNoArg();
using(mocks.Unordered()
{
demo.VoidNoArg();
demo.IntNoArg();
}
demo.IntNoArg();
}
//Must exit the ordering before calling
mocks.ReplayAll();
//The expectations we set up is:
// 1. demo.VoidNoArgs();
// 2. in any order:
// 1. demo.VoidNoArg();
// 2. demo.IntNoArg();
// 3. demo.IntNoArg();
demo.VoidNoArg();
demo.IntNoArg();
demo.VoidNoArg();
demo.IntNoArg();
}

CreateMock

Create a mock object with strict semantics.  Strict semantics means that any call that wasn’t explicitly recorded is considered an error and would cause an exception to be thrown.

DynamicMock

Create a mock object with dynamic semantics.  Dynamic semantics means that any call that wasn’t explicitly recorded is accepted and a null or zero is returned (if there is a return value).

PartialMock

Create a mock object with from a class that defaults to calling the class methods if no expectation is set on the method.

Replay

Moves a single mock object to the replay state.  This method cannot be called from inside an ordering.

BackToRecord

Moves the mocked object back to record state.  This works on mock objects regardless of state.  You can (and it’s recommended) to run Verify() before you use this method, but it’s not neccecary.

Note

This will remove all the current expectations from the mock repository.

Verify

Verifies that all expectations has been met for a single mock object.  After calling this method and action taken on the mock object would result in an exception even if the object is a dynamic mock.

LastMethodCall

Gets the method options for the last call on mockedInstance

MethodCall

Handles a method call for a mock object.

ReplayAll

Moves all the mock objects in the repository to replay state.

Note

This method will skip any mock object that you’ve manually moved to replay state by calling Replay

VerifyAll

Verifies that all the expectations on all mock objects in the repository are met.

Note

This method skip any mock objects that you’ve manually verified using Verify

Exception safety

If an unexpected exception has been thrown (which would fail the test) and the Repository still have unsatisfied expectations, this method will cause another exception, that may mask the real cause.  If this happens to you, you may need to avoid the using statement until you figure out what is wrong: The using statement:

using(MockRepository mocks = new MockRepository())
{
// Some action that cause an unexpected exception
// which would cause unsatisfied expectation and cause
// VerifyAll() to fail.
}

The unrolled using statement

MockRepository mocks = new MockRepository())
//The afore mentioned action
mocks.VerifyAll()//won't occur if an exception is thrown

This way you can get the real exception from the unit testing framework.

Properties

Replayer

The replayer for the repository.

LastProxy

The last mock object that was called on any repository.

Create a new instance of MockRepository
Allows to set options for the method calls.
Verifies that all expectations has been met for a single mock object.
Moves a single mock object to the replay state.