How to Mock Singletons in 3 Simple Ways
Want to mock Singletons?
Here are 3 simple ways to mock a Singletons.
1. Use MockAll
When the static constructor is not mocked just use MockAll
Mock theSingletonMock = MockManager.MockAll(typeof(Singleton), Constructor.StaticNotMocked);
or to mock the constructor
Mock theSingletonMock = MockManager.MockAll(typeof(Singleton), Constructor.NotMocked);
Remember that you can only MockAll once until all mocks are verified/cleared. To retrieve the mock simply use:
Mock theSingletonMock = MockManager.GetMockAll(typeof(Singleton));
2. Use a MockObject
When the static constructor needs to be mocked we can return a mocked object whenever Instance is called
// make sure that all constructors are mocked MockManager.MockAll(typeof(Singleton), Constructor.Mocked); // create mocked singleton objectMockObject theSingletonMock = MockManager.MockObject(typeof(Singleton), Constructor.Mocked); // return our mocked singleton theSingletonMock.ExpectGetAlways("Instance", theSingletonMock.Object);
Note: This will only work when the static constructor is mocked.
Also watch out for beforeFieldInit issues.
3. Set the instance Field
Another way to mock static constructors of singletons is to set the instance field.
MockObject theSingletonMock = MockManager.MockObject(typeof(Singleton), Constructor.Mocked); ObjectState.SetField(typeof(Singleton), "instance", theSingletonMock.Object);
remember that you are changing a field so it is best to reset the field after the test. Here is one way.
MockObject theSingletonMock = MockManager.MockObject(typeof(Singleton), Constructor.Mocked); object keptSingleton = ObjectState.GetField(typeof(Singleton), "instance"); try { ObjectState.SetField(typeof(Singleton), "instance", theSingletonMock.Object); // the test... } finally { ObjectState.SetField(typeof(Singleton), "instance", keptSingleton); }
Note: As before this will only work when the static constructor is mocked.
1 Comment to “How to Mock Singletons in 3 Simple Ways”
Recent Posts
- Top 5 questions to ask when checking references
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006

gxvvyq0jzestrdwc