8

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

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 设置的单元测试。

任何人都可以帮助我解决上述方法的模拟对象的最佳测试方法是什么?

4

1 回答 1

11

如果ProductService要先模拟,则需要注入此依赖项。

构造函数注入是 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(),

            //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 */
    }
}
于 2013-11-17T18:55:14.633 回答