How to Mock Singletons in 3 Simple Ways
来源:互联网 发布:淘宝店铺图片轮播尺寸 编辑:程序博客网 时间:2024/06/11 05:44
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 mockedMockManager.MockAll(typeof(Singleton), Constructor.Mocked);// create mocked singleton object MockObject 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.
- How to Mock Singletons in 3 Simple Ways
- How to enforce Checkstyle in SVN commits : Simple Guide
- How to create a simple Ad Hoc Reporting in HR ?
- How To Create Buttons in Cocos2D: Simple, Radio, and Toggle
- How to configure a simple WebDAV in apache
- How To Be More Interesting (In 10 Simple Steps)
- How to write a simple interpreter in JavaScript
- 5 simple ways to troubleshoot using Strace
- 5 simple ways to troubleshoot using Strace
- [gist]How to use Mock testing Python
- how to learn unit test mock framework
- Different ways how to escape an XML string in C# (zz)
- Singletons in Objective-C
- Singletons in Objective-C
- The most simple and easy to use Qt GUI library plug-in FTStyle (3)How do Qt4 and Qt5 choose
- A poor simple sample to perfectly introduce how to program in CUDA
- 3 ways to draw 3d lines in unity3d
- 3 Ways to Kill an Application/Activity/Process in Android
- 迟到的博客
- hql关联和连接查询实例
- linux基础--开机自启动
- 设置QPushButton的背景图片(或者Window、WindowText等的背景图片/颜色)
- Perl语言学习笔记 14 字符串与排序
- How to Mock Singletons in 3 Simple Ways
- ACM 139. [USACO Feb08] 麻烦的聚餐(dp+递推)
- c语言中调用shell脚本
- POJ—3069—Saruman's Army—【贪心】
- Max Points on a Line
- Codeforces Round #262 (Div. 2)B. Little Dima and Equation
- c++学习论坛
- IOS中定时器NSTimer的开启与关闭
- JAVA文件读写输入输出流关闭问题、JAVA垃圾回收