问题描述
我正在ASP.Net Web应用程序中进行单元测试,现在我已访问模型文件中的构造函数以测试其中具有用于上传XMLfile的Server.MapPath代码,当尝试对此进行测试时,我得到了错误,因为HttpContext为null,因此我必须模拟Server.MapPath.
I have working in unit testing in ASP.Net web application, now I have accessing my constructor in the model file to test which has Server.MapPath code for uploading my XMLfile, when try to testing this i get error, because the HttpContext is null so i have to mocking Server.MapPath.
我进行了很多搜索,但每个样本仅针对Asp.NET MVC,但我使用的是ASP.NET.因此,请在ASP.NET中提供帮助以解决此问题.
I have searched lot but every samples given only for Asp.NET MVC but I have working in ASP.NET. so please help in ASP.NET to solve this issue.
我的代码如下.
public class NugetPlatformModel
{
public bool IsHavingLicense { get; set; }
public List<PlatformProducts> PlatformProduct = new List<PlatformProducts>();
public NugetPlatformModel()
{
var xmldoc = new XmlDocument();
mldoc.Load(HttpContext.Current.Server.MapPath(@"~\Content\PlatformProducts.xml"));
}
}
还有我的单元测试代码
[Test]
public void Account_UnlicensedCustomerIdentity_IsStudioLicenseAndIshavinglicenseFalse()
{
//Act
NugetPlatformModel nugetPlatformModel = new NugetPlatformModel();
//Assert
AssertEquals(false, nugetPlatformModel.IsHavingLicense);
}
推荐答案
这在调用静态方法的代码中很常见,在保持关注点分离和避免紧密耦合的同时进行测试非常困难.这是测试和模拟"unestable代码"的通用方法:为其编写外观包装".
This is typical with code that calls static methods, it's very difficult to test while preserving separation of concern and avoiding tight coupling. Here is a generic approach to test and mock "untestable code": write a "facade wrapper" to it.
-
为这些方法创建包装.一个简单的类,其中包含明智地命名的方法,并且仅委托给不可测试的调用(通常是静态调用)
Create a wrapper for these methods. A simple class that contains methods named sensibly, and only delegates to the untestable calls (typically static calls)
创建该包装类的接口
而不是直接在客户端代码中调用无法测试的方法,请使用包装器(使用步骤2中提供的接口进行依赖注入)并在其上调用常规方法.
Instead of directly calling the untestable methods in your client code, use the wrapper (dependency-injected using the interface provided in step 2) and call normal methods on it.
在单元测试中,使用所需的行为模拟包装器.
In your unit-test, mock the wrapper with the behaviour you want.
这种方法有效地减少了耦合并分离了需要分离的关注点.当然,您仍然不能测试包装器本身的行为,但是,如果它足够简单(仅委托给原始调用),那么问题就不大了.
This approach effectively reduces the coupling and separates the concerns that need to be separated. Of course, you still can't test the behaviour of the wrapper itself, but if it's simple enough (only delegating to the original calls) then it's not as big a problem.
更新:
Shim类型是Microsoft Fakes Framework使用的两种技术之一,可让您轻松地将测试中的组件与环境隔离. Shims将对特定方法的调用转移到您在测试中编写的代码.许多方法根据外部条件返回不同的结果,但是填充程序在测试的控制下,并且可以在每次调用时返回一致的结果.这使您的测试更容易编写.使用垫片将代码与不属于解决方案的程序集隔离.为了将解决方案的各个组件相互隔离,我们建议您使用存根.
Shim types are one of two technologies that the Microsoft Fakes Framework uses to let you easily isolate components under test from the environment. Shims divert calls to specific methods to code that you write as part of your test. Many methods return different results dependent on external conditions, but a shim is under the control of your test and can return consistent results at every call. This makes your tests much easier to write.Use shims to isolate your code from assemblies that are not part of your solution. To isolate components of your solution from each other, we recommend that you use stubs.
这篇关于在ASP.NET单元测试中模拟HttpContext.server.MapPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!