使用单例进行单元测试 [英] Unit testing with singletons

查看:44
本文介绍了使用单例进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Visual Studio Team Edition 测试框架准备了一些自动测试.我希望其中一个测试按照在程序中完成的正常方式连接到数据库:

I have prepared some automatic tests with the Visual Studio Team Edition testing framework. I want one of the tests to connect to the database following the normal way it is done in the program:

string r_providerName = ConfigurationManager.ConnectionStrings["main_db"].ProviderName;

但是我在这一行收到了一个异常.我想这是因为 ConfigurationManager 是一个单例.您如何通过单元测试解决单例问题?

But I am receiving an exception in this line. I suppose this is happening because the ConfigurationManager is a singleton. How can you work around the singleton problem with unit tests?

感谢您的回复.他们都非常有指导意义.

Thanks for the replies. All of them have been very instructive.

推荐答案

查看 Google 测试博客:

还有:

最后,Misko Hevery 在他的博客上写了一篇指南:编写可测试代码.

Finally, Misko Hevery wrote a guide on his blog: Writing Testable Code.

这篇关于使用单例进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆