单元测试 – 使用MOQ对象进行ASP.NET MVC单元测试
发布时间:2020-12-16 04:34:58 所属栏目:asp.Net 来源:网络整理
导读:在单元测试中模拟以下代码的最佳方法是什么: public ActionResult Products(){ ViewBag.Title = "Company Product"; IEnumerableProductDetailDto productList = ProductService.GetAllEffectiveProductDetails(); ProductModels.ProductCategoryListModel
在单元测试中模拟以下代码的最佳方法是什么:
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个人资料?
- asp.net-mvc – 如何从MVC WEB API Controller返回JSON
- asp.net-mvc – 使用OWIN的Google身份验证Oauth在MVC5中没有
- asp.net-web-api – Web API中的Actions和QueryString参数
- 记一次OAuth碰到的问题
- asp.net-mvc-3 – 在IIS 7 / 7.5中注册PATCH HTTP谓词
- asp.net-mvc – ASP.NET Core TestServer为Razor视图生成HT
- asp.net-mvc – 如何将数据从AuthorizeAttribute传递给Cont
- asp.net-identity – 支持EF和Asp.NEt身份的IdentityServer
- asp.net – 带有requireSSL for cookies的Sitecore
推荐文章
站长推荐
- asp.net-web-api – 如何只获取没有值的Odata.Co
- asp.net-mvc – 使用和新Relic进行Azure部署
- asp.net-mvc – 使用ASP.NET MVC Ajax调用的全局
- 谈谈基于SQL Server 的Exception Handling[中篇]
- asp.net – System.Linq.Dynamic不支持OrderByDe
- ado.net – ASP.NET:如何从web.config Connecti
- asp.net-mvc-4 – bundle(Sytem.Web.Optimizatio
- asp.net-mvc – 如何使用MVC调用JQuery UI模式(C
- asp.net-web-api – WebAPI:403在发布网站后被禁
- asp.net-mvc – 在请求之间保持/缓存数据 – 常用
热点阅读