Click or drag to resize
TestUtilitiesCreateMockSingletonT Method

Namespace: Test.Framework
Assembly: Test.Framework (in Test.Framework.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public static Mock<T> CreateMockSingleton<T>()
where T : class

Type Parameters

T

Return Value

Type: MockT
Remarks

The application has adopted the idea that singletons are denoted by the ISingletonT interface. This declares a property which returns the same singleton reference regardless of the instance that it's called from. We need to make sure that any tests will pick up the accidental use of a private instance of the class. For example we want to make sure that if the user does this:

var log = Factory.Singleton.Resolve<ILog>();
log.DoSomething();

instead of this:

var log = Factory.Singleton.Resolve<ILog>().Singleton;
log.DoSomething();

then the test will throw an exception. We do this by creating a strict mock that only has Singleton setup for it, so any call on it other than to read Singleton will cause the mock to throw an exception and fail the test. This strict mock is then registered with the class factory.

The Singleton property of the strict mock is then set up to return a friendly mock, and it is this mock that gets returned by the method. The test can configure this mock with the correct behaviour and rest assured that the code under test could only get a reference to this mock by asking the class factory to resolve the interface and then reading the Singleton property of the object that the class factory dished up.

See Also