【发布时间】:2013-11-12 10:32:47
【问题描述】:
在单元测试中模拟以下代码的最佳方法是什么:
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 设置进行单元测试。
任何人都可以帮助我解决上述方法的模拟对象的最佳测试方法是什么?
【问题讨论】:
-
我猜你可能想创建一个 Moq
System.Web.HttpContextBase(根据需要设置 Moq 用户、请求、响应、会话、缓存、服务器等)。 -
是的,我想创建 MOQ 对象。
-
需要更多信息。您是否正在尝试为上述方法编写单元测试?而您试图模拟诸如 ProductService 之类的依赖项?您提到了“在单元测试中模拟以下代码的最佳方法是什么”。你到底想在这里模拟什么下面的代码?
-
>>是的,我正在尝试使用最小起订量编写单元测试。 >>我想知道如何在我的示例中模拟像 ProductService 这样的依赖项?
标签: unit-testing asp.net-mvc-4 controller moq