简体   繁体   English

单元测试在Web应用程序使用的类库中使用configurationmanager的方法

[英]Unit testing a method that uses configurationmanager in a class library used by a web app

I'm attempting to write a test for the code behind a web service. 我正在尝试为Web服务背后的代码编写测试。 This code lives in a separate class library called from the service itself. 此代码位于从服务本身调用的单独的类库中。 This class library uses ConfigurationManager to get its connection strings, normally when you run the web service those are populated by the web service's web.config . 此类库使用ConfigurationManager获取其连接字符串,通常在运行Web服务时,由Web服务的web.config填充。 However, when I run it from my test code they throw null exceptions. 但是,当我从测试代码运行它时,它们会抛出null异常。

I've trolled around the net for answers for a while now and attempted several fixes for roughly similar problems, but nothing worked. 我已经绕过网络寻找答案了一段时间,并尝试了几个类似问题的修复,但没有任何效果。 (Nothing was exactly my situation.) (没有什么是我的情况。)

It depends on the version of Visual Studio that you have, but if you have VS 2010 you can use Moles to mock static classes and in VS 2012 you have Microsoft Fakes . 这取决于您拥有的Visual Studio版本,但是如果您有VS 2010,则可以使用Moles来模拟静态类,而在VS 2012中,您可以使用Microsoft Fakes

Nevertheless, as Joachim Isaksson said in his comment, if you are actually doing a Unit Test, you could wrap access ConfigurationManager to the through an interface and a wrapper, and just mock the interface. 然而,正如Joachim Isaksson在他的评论中所说,如果你实际上正在进行单元测试,你可以通过一个接口和一个包装器来访问ConfigurationManager,并且只是模拟接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM