Table of Contents

Class MockRepository

Namespace
Moq
Assembly
Moq.dll

Utility repository class to use to construct multiple mocks when consistent verification is desired for all of them.

public class MockRepository : MockFactory
Inheritance
MockRepository
Inherited Members

Examples

The following is a straightforward example on how to create and automatically verify strict mocks using a MockRepository:

var repository = new MockRepository(MockBehavior.Strict);

var foo = repository.Create<IFoo>();
var bar = repository.Create<IBar>();

// no need to call Verifiable() on the setup 
// as we'll be validating all of them anyway.
foo.Setup(f => f.Do());
bar.Setup(b => b.Redo());

// exercise the mocks here

repository.VerifyAll(); 
// At this point all setups are already checked 
// and an optional MockException might be thrown. 
// Note also that because the mocks are strict, any invocation 
// that doesn't have a matching setup will also throw a MockException.

The following examples shows how to setup the repository to create loose mocks and later verify only verifiable setups:

var repository = new MockRepository(MockBehavior.Loose);

var foo = repository.Create<IFoo>();
var bar = repository.Create<IBar>();

// this setup will be verified when we verify the repository
foo.Setup(f => f.Do()).Verifiable();

// this setup will NOT be verified 
foo.Setup(f => f.Calculate());

// this setup will be verified when we verify the repository
bar.Setup(b => b.Redo()).Verifiable();

// exercise the mocks here
// note that because the mocks are Loose, members 
// called in the interfaces for which no matching
// setups exist will NOT throw exceptions, 
// and will rather return default values.

repository.Verify();
// At this point verifiable setups are already checked 
// and an optional MockException might be thrown.

The following examples shows how to setup the repository with a default strict behavior, overriding that default for a specific mock:

var repository = new MockRepository(MockBehavior.Strict);

// this particular one we want loose
var foo = repository.Create<IFoo>(MockBehavior.Loose);
var bar = repository.Create<IBar>();

// specify setups

// exercise the mocks here

repository.Verify();

Remarks

If multiple mocks will be created during a test, passing the desired MockBehavior (if different than the Default or the one passed to the repository constructor) and later verifying each mock can become repetitive and tedious.

This repository class helps in that scenario by providing a simplified creation of multiple mocks with a default MockBehavior (unless overridden by calling Create<T>(MockBehavior)) and posterior verification.

Constructors

MockRepository(MockBehavior)

Initializes the repository with the given defaultBehavior for newly created mocks from the repository.

public MockRepository(MockBehavior defaultBehavior)

Parameters

defaultBehavior MockBehavior

The behavior to use for mocks created using the Create<T>() repository method if not overridden by using the Create<T>(MockBehavior) overload.

See Also

Methods

Of<T>()

Access the universe of mocks of the given type, to retrieve those that behave according to the LINQ query specification.

public IQueryable<T> Of<T>() where T : class

Returns

IQueryable<T>

Type Parameters

T

The type of the mocked object to query.

See Also

Of<T>(MockBehavior)

Access the universe of mocks of the given type, to retrieve those that behave according to the LINQ query specification.

public IQueryable<T> Of<T>(MockBehavior behavior) where T : class

Parameters

behavior MockBehavior

Behavior of the mocks.

Returns

IQueryable<T>

Type Parameters

T

The type of the mocked object to query.

See Also

Of<T>(Expression<Func<T, bool>>)

Access the universe of mocks of the given type, to retrieve those that behave according to the LINQ query specification.

public IQueryable<T> Of<T>(Expression<Func<T, bool>> specification) where T : class

Parameters

specification Expression<Func<T, bool>>

The predicate with the setup expressions.

Returns

IQueryable<T>

Type Parameters

T

The type of the mocked object to query.

See Also

Of<T>(Expression<Func<T, bool>>, MockBehavior)

Access the universe of mocks of the given type, to retrieve those that behave according to the LINQ query specification.

public IQueryable<T> Of<T>(Expression<Func<T, bool>> specification, MockBehavior behavior) where T : class

Parameters

specification Expression<Func<T, bool>>

The predicate with the setup expressions.

behavior MockBehavior

Behavior of the mocks.

Returns

IQueryable<T>

Type Parameters

T

The type of the mocked object to query.

See Also

See Also