如何嘲弄与起订量ConfigurationManager.AppSettings [英] How to mock ConfigurationManager.AppSettings with moq

查看:148
本文介绍了如何嘲弄与起订量ConfigurationManager.AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我停留在这一点上的代码,我不知道如何嘲笑:

I am stuck at this point of code that I do not know how to mock:

ConfigurationManager.AppSettings["User"];



我嘲笑ConfigurationManager中,但我没有线索,我使用<一个HREF =htt​​p://code.google.com/p/moq/。>起订量。

有人可以给我一个提示?谢谢!

Someone can give me a tip? Thanks!

推荐答案

我相信这一个标准的方法是使用的门面的模式来包装配置管理器,然后你有一些松耦合的,你有控制权。

I believe one standard approach to this is to use a facade pattern to wrap the configuration manager and then you have something loosely coupled that you have control over.

所以,你会包装ConfigurationManager中。是这样的:

So you would wrap the ConfigurationManager. Something like:

public class Configuration: IConfiguration
{
    public User
    {
        get{ 
               return ConfigurationManager.AppSetting["User"];
       }
    }
}



(你可以只提取一个从配置类的接口,然后使用该接口无处不在您的代码)
然后你只需嘲笑IConfiguration。你也许能够实现在几个不同的方式门面本身。上面我选择来包装各个属性。您也获得具有强类型的信息,而与工作不是弱类型的哈希阵列附带的好处。

(You can just extract an interface from your configuration class and then use that interface everywhere in your code) Then you just mock the IConfiguration. You might be able to implement the facade itself in a few different ways. Above I chose just to wrap the individual properties. You also obtain the side benefit of having strongly typed information to work with rather than weakly typed hash arrays.

这篇关于如何嘲弄与起订量ConfigurationManager.AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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