Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 使用MOQ对象进行ASP.NET MVC单元测试_Unit Testing_Asp.net Mvc 4_Controller_Moq - Fatal编程技术网

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

Unit testing 使用MOQ对象进行ASP.NET MVC单元测试,unit-testing,asp.net-mvc-4,controller,moq,Unit Testing,Asp.net Mvc 4,Controller,Moq,在单元测试中模拟以下代码的最佳方法是什么: public ActionResult Products() { ViewBag.Title = "Company Product"; IEnumerable<ProductDetailDto> productList = ProductService.GetAllEffectiveProductDetails(); ProductModels.Prod

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

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);
}
public ActionResult产品()
{
ViewBag.Title=“公司产品”;
IEnumerable productList=ProductService.GetAllEffectiveProductDetails();
ProductModels.ProductCategoryListModel模型=新ProductModels.ProductCategoryListModel
{     
//ProductDetails的类型=>IEnumerable
ProductDetails=ProductService.GetAllEffectiveProductDetails(),
//ProductCategoryList的类型=>IEnumerable
ProductCategoryList=productList.Select(x=>new SelectListItem
{
Value=x.FKProductId.ToString(),
Text=x.名称
})
};
返回视图(模型);
}
仅供参考,我正在进行VS 2012、MVC 4.0、使用MOQ对象和TFS设置的单元测试


有人能帮我解决这个问题吗?对于上述方法,使用mock对象的最佳测试方法是什么?

如果要模拟
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

我想你可能想创建一个Moq
System.Web.HttpContextBase
(根据需要设置Moq用户、请求、响应、会话、缓存、服务器等)。是的,我想创建Moq对象。需要更多信息。您是否正在尝试为上述方法编写单元测试?您是否试图模拟ProductService之类的依赖项?您提到“在单元测试中模拟下面代码的最佳方法是什么”。您到底想在这里模拟下面的代码是什么?>>是的,我正在尝试使用moq编写单元测试。>>我想知道如何在我的示例中模拟ProductService之类的依赖项?我也会这样做+1但在生产中,MVC框架会自动调用控制器的构造函数和IProduceService实例吗?为了使MVC自动调用控制器的构造函数和
IProduceService
实例,需要实现自定义控制器工厂,否则您将得到。下面的链接包含一个实现示例。
[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 */
    }
}