gpt4 book ai didi

unit-testing - 使用 MOQ 对象进行 ASP.NET MVC 单元测试

转载 作者:行者123 更新时间:2023-12-04 05:23:12 24 4
gpt4 key购买 nike

在单元测试中模拟以下代码的最佳方法是什么:

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首先你需要注入(inject)这个依赖。

Constructor injection是 ASP.NET MVC 中 Controller 最常用的方法。

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(),

//the type of ProductCategoryList => IEnumerable<selectlistitem>
ProductCategoryList = productList.Select(x => new SelectListItem
{
Value = x.FKProductId.ToString(),
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及其 model .
[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 */
}
}

关于unit-testing - 使用 MOQ 对象进行 ASP.NET MVC 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19926743/

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