gpt4 book ai didi

c# - 如何对使用静态类的方法进行单元测试,而静态类又使用 ConfigurationElementCollection?

转载 作者:行者123 更新时间:2023-11-30 20:46:38 24 4
gpt4 key购买 nike

public class ConfigSection : ConfigurationSection
{
public static ConfigSection GetConfigSection()
{
return (ConfigSection)System.Configuration.ConfigurationManager.
GetSection("ConfigSections");
}

[System.Configuration.ConfigurationProperty("ConstantsSettings")]
public ConstantSettingCollection ConstantsSettings
{
get
{
return (ConstantSettingCollection)this["ConstantsSettings"] ??
new ConstantSettingCollection();
}
}


public class ConstantSettingCollection : ConfigurationElementCollection
{

public ConstantElements this[object key]
{
get
{
return base.BaseGet(key) as ConstantElements;
}
set
{
if (base.BaseGet(key) != null)
{
base.BaseRemove(key);
}
this.BaseAdd(this);
}
}

protected override ConfigurationElement CreateNewElement()
{
return new ConstantElements();
}

protected override object GetElementKey(ConfigurationElement element)
{
return ((ConstantElements)element).Key;
}
}

public class ConstantElements : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get
{
return this["key"] as string;
}
}

[ConfigurationProperty("val", IsRequired = true)]
public string Constants
{
get { return this["value"] as string; }
}
}
}

public class ConstantHelper
{
public static string ConstantForLog
{
get
{
return ConfigSection.GetConfigSection().ConstantsSettings["ConstantForLog"].Constants;
}
}
}

上面的单元测试是从应用程序配置中读取一些常量值的代码这是我在构造函数中的代码已分配的值。

public class HomeController
{
protected string constants;
public HomeController()
{
constants = ConstantHelper.ConstantForLog;
}
}

测试代码

[TestClass]
public class HomeControllerTester
{
[TestMethod]
public void Initialize_Tester()
{
//Creating Instance for the HomeController
HomeController controller = new HomeController();
}
}

调试时发现 Appsettings 没有被 ConstantHelper 类读取

解决方案

找到解决方案实际上它工作正常错误是在 app.config 中完成的

我遇到的另一个问题是 ConfigSection对于 MVC 应用程序 web.config,不需要命名空间 type="type"对于单元测试 app.config,需要命名空间 type="type,_namespace"

最佳答案

您必须将 ConstantHelper 注入(inject)到 HomeController 中。您可以将其接口(interface)出来,然后将其注入(inject)。从单元测试传递 IConstantHelper 的模拟对象。

更新

我已经为 ConstantHelper 类定义了一个接口(interface),让我有机会注入(inject)和模拟依赖项。

ConstantHelper.cs

public class ConstantHelper : IConstantHelper
{
public string ConstantForLog
{
get
{
return ConfigSection.GetConfigSection().ConstantsSettings["ConstantForLog"].Constants;
}
}
}

public interface IConstantHelper
{
string ConstantForLog { get; }
}

HomeController.cs

请注意,我现在从外部注入(inject)常量助手,以便单元测试可以模拟它。

public class HomeController : Controller
{
private readonly IConstantHelper _constantHelper;

public HomeController(IConstantHelper constantHelper)
{
_constantHelper = constantHelper;
}

public ActionResult Index()
{
return View(_constantHelper.ConstantForLog);
}
}

HomeControllerTest.cs

[TestClass]
public class HomeControllerTest
{
[TestMethod]
public void Index_WithDependecySetupCorrectly_ReturnTestString()
{
var mockHelper = new Mock<IConstantHelper>();
const string testDataString = "TestString";
mockHelper.Setup(z => z.ConstantForLog).Returns(testDataString);

//Creating Instance for the HomeController
var controller = new HomeController(mockHelper.Object);

var result = controller.Index() as ViewResult;

Assert.IsNotNull(result);
Assert.AreEqual(testDataString, result.ViewName);
}
}

我使用 Moq 模拟框架。只需在测试项目的包管理器控制台中使用以下命令安装它:

Install-Package Moq

希望这对您有所帮助。

关于c# - 如何对使用静态类的方法进行单元测试,而静态类又使用 ConfigurationElementCollection?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com