单元测试 – 使用MOQ对象进行ASP.NET MVC单元测试
发布时间:2020-05-24 09:57:51 所属栏目:asp.Net 来源:互联网
导读:在单元测试中模拟以下代码的最佳方法是什么: public ActionResult Products(){ ViewBag.Title = Company Product; IEnumerableProductDetailDto productList = ProductService.GetAllEffecti
|
在单元测试中模拟以下代码的最佳方法是什么: public ActionResult Products()
{
ViewBag.Title = "Company Product";
IEnumerable<ProductDetailDto> productList = ProductService.GetAllEffectiveProductDetails();
ProductModels.ProductCategoryListModel model = new ProductModels.ProductCategoryListModel
{
//the type of ProductDetails => IEnumerable<productDetailDto>
ProductDetails = ProductService.GetAllEffectiveProductDetails(),//the type of ProductCategoryList => IEnumerable<selectlistitem>
ProductCategoryList = productList.Select(x => new SelectListItem
{
Value = x.FKProductId.ToString(),Text = x.Name
})
};
return View(model);
}
仅供参考,我正在研究VS 2012,MVC 4.0,使用MOQ对象和TFS设置进行单元测试. 任何人都可以帮我解决这个问题,对于上述方法,使用模拟对象的最佳测试方法是什么? 解决方法如果您想首先模拟ProductService,则需要注入此依赖项.Constructor injection是ASP.NET MVC中最常用的控制器方法. public class YourController : Controller
{
private readonly IProductService ProductService;
/// <summary>
/// Constructor injection
/// </summary>
public YourController(IProductService productService)
{
ProductService = productService;
}
/// <summary>
/// Code of this method has not been changed at all.
/// </summary>
public ActionResult Products()
{
ViewBag.Title = "Company Product";
IEnumerable<ProductDetailDto> productList = ProductService.GetAllEffectiveProductDetails();
ProductModels.ProductCategoryListModel model = new ProductModels.ProductCategoryListModel
{
//the type of ProductDetails => IEnumerable<productDetailDto>
ProductDetails = ProductService.GetAllEffectiveProductDetails(),Text = x.Name
})
};
return View(model);
}
}
#region DataModels
public class ProductDetailDto
{
public int FKProductId { get; set; }
public string Name { get; set; }
}
public class ProductModels
{
public class ProductCategoryListModel
{
public IEnumerable<ProductDetailDto> ProductDetails { get; set; }
public IEnumerable<SelectListItem> ProductCategoryList { get; set; }
}
}
#endregion
#region Services
public interface IProductService
{
IEnumerable<ProductDetailDto> GetAllEffectiveProductDetails()
}
public class ProductService : IProductService
{
public IEnumerable<ProductDetailDto> GetAllEffectiveProductDetails()
{
throw new NotImplementedException();
}
}
#endregion
然后,您可以轻松地创建IProductService的模拟实例,将其传递给YourController的构造函数,设置GetAllEffectiveProductDetails方法并检查返回的ActionResult及其模型. [TestClass]
public class YourControllerTest
{
private Mock<IProductService> productServiceMock;
private YourController target;
[TestInitialize]
public void Init()
{
productServiceMock = new Mock<IProductService>();
target = new YourController(
productServiceMock.Object);
}
[TestMethod]
public void Products()
{
//arrange
// There is a setup of 'GetAllEffectiveProductDetails'
// When 'GetAllEffectiveProductDetails' method is invoked 'expectedallProducts' collection is exposed.
var expectedallProducts = new List<ProductDetailDto> { new ProductDetailDto() };
productServiceMock
.Setup(it => it.GetAllEffectiveProductDetails())
.Returns(expectedallProducts);
//act
var result = target.Products();
//assert
var model = (result as ViewResult).Model as ProductModels.ProductCategoryListModel;
Assert.AreEqual(model.ProductDetails,expectedallProducts);
/* Any other assertions */
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 如何使用剃刀包括.html或.asp文件?
- ASP.NET MVC – 返回JavaScriptResult不起作用
- asp.net-mvc-4 – 如何使用SimpleMembership管理配置文件?
- asp.net-mvc – 在MVC中写数据库和业务逻辑的位置?
- asp.net – 如果页面上有异常,则输出缓存无效
- 您希望在初学者的ASP.NET安全手册中看到什么?
- asp.net-mvc – Web API和ASP MVC之间的主要区别是什么
- asp.net – Cookies和子域名
- 强制CamelCase在ASP.NET WebAPI每个控制器
- asp.net-mvc – 如何将ASP.Net MVC路径段中的1或0映射到布尔
推荐文章
站长推荐
- 关闭一个子目录的ASP.Net WebForms身份验证
- asp.net-mvc – 支持RavenDB的MVC4的会员系统
- 如何在ASP.Net(MVC)中避免XSS漏洞?
- ASP.NET缓存的方法和最佳实践
- asp.net – 通过Ajax Post – MVC3更新模型更改视
- asp.net成员资格 – 在Application_Authenticati
- 在ASP.NET MVC视图中渲染HTML文件?
- asp.net – IIS Express(WebMatrix)打开外部连接
- asp.net-mvc – 验证:Model或ViewModel
- ASP.NET – 如何在User Control中使用Response.R
热点阅读
