测试方法参照的Web.Config在.net C# [英] Testing Methods with Reference to Web.Config in .Net C#

查看:115
本文介绍了测试方法参照的Web.Config在.net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,还是没能找到该固溶体。假设你在你的应用程序的方法。这种方法使用System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration访问web.config中的一些设置。如果你尝试测试这些方法,你的测试将失败,因为你的测试项目没有web.config文件。

I searched a lot and still couldn't find a solid solution for this. Suppose you have methods in your application. This methods use "System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration" to access some setting in the web.config. If you try to test these methods, your tests will fail because your test project doesn't have web.config.

这是解决这个问题的最佳方法。对于简单的配置文件的项目,我通常使用的方法是这样的门面方法。

What is the best way to solve this problem. For projects with simple config file, I usually use a method like this as facade method.

public class Config
{
    public static String getKeyValue(String keyName)
    {
        if (keyName == String.Empty) return String.Empty;
        String result = "";

        System.Configuration.Configuration rootWebConfig1 =
           System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
        if (rootWebConfig1.AppSettings.Settings.Count > 0)
        {
            System.Configuration.KeyValueConfigurationElement reportEngineKey =
                rootWebConfig1.AppSettings.Settings[keyName];

            if (reportEngineKey != null)
            {
                result = reportEngineKey.Value;

            }

        }

        return result;
    }
}

每次我试图为OpenWebConfiguration()的路径,我得到了错误相对虚拟路径是不允许的

Every time I tried to set the path for OpenWebConfiguration( ), I got the error "The relative virtual path is not allowed"

推荐答案

要作出这样的情况下更容易测试,我通常做我自己的设置管理器,并给它一个接口的方法。因此,例如:

To make that scenario more testable, I usually take the approach of making a "settings manager" of my own, and giving it an interface. So for example:

public interface IConfig
{
    string GetSettingValue(string settingName);
}

然后我可以有我的真正的实施

Then I can have my "real" implementation:

public sealed class Config : IConfig
{
    public string GetSettingValue(string settingName)
    {
        // your code from your getKeyValue() method would go here
    }
}

然后我使用它会采取在这方面的一个实例(这是的依赖倒置主要):

public void DoStuff(IConfig configuration)
{
    string someSetting = configuration.GetSettingValue("ThatThingINeed");
    // use setting...
}

所以,现在我生产code,我可以叫 DoStuff ,并通过在配置的一个实例。
当我需要测试,我可以用嘲弄的工具(MOQ,JustMock,RhinoMocks的等),以创建一个假 IConfig ,如果没有击中实际的.config返回一个已知值文件,或者你可以不用通过自己的嘲弄嘲弄框架(并存储在您的测试项目)。

So now for my production code, I can call DoStuff and pass in an instance of Config. When I need to test, I can use a mocking tool (Moq, JustMock, RhinoMocks, etc) to create a fake IConfig that returns a known value without hitting the actual .config file, or you can do it without a mocking framework by making your own mocks (and store them in your test project).

public class ConfigMock : IConfig
{
    private Dictionary<string, string> settings;

    public void SetSettingValue(string settingName, string value)
    {
        settings[settingName] = value;
    }

    public string GetSettingValue(string settingName)
    {
        return settings[settingName];
    }
}

[Test]
public void SomeExampleTest()
{
    var config = new ConfigMock();
    config.SetSettingValue("MySetting", "SomeValue");

    var underTest = new MyClass();
    underTest.DoStuff(config);
}

这篇关于测试方法参照的Web.Config在.net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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