ASP.NET Web API 2 - 简单Unit Testing
来源:互联网 发布:护肤品淘宝店名字大全 编辑:程序博客网 时间:2024/06/01 07:35
资源:http://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api
本文讲述的是为Web API 2 app创建简单的unit test。如何在solution中创建unit test,如何编写用来检查从controller方法中返回的值的test method(测试方法)。这里展示的仅仅是简单的数据场景,更多高级数据场景参考:ASP.NET Web API 2 - 模拟 EF(Entity Framework) 的Unit Testing。
创建Unit Test 项目
这里有两种方法:
创建包含Unit Test的app
在已存在的app中添加Unit Test
在solution上右键-> Add-> New Project-> Test-> Unit Test
创建Web API 2 app
在Models下添加Products.cs后,build这个solution。
public class Product { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } }在Controllers下添加ProductController
public class ProductController : ApiController { List<Product> products = new List<Product>(); //Refer to as DataBase public ProductController() { } public ProductController(List<Product> products ) //The constructor enables Unit Test to pass test data { this.products = products; } public IEnumerable<Product> GetAllProducts() { return products; } public async Task<IEnumerable<Product>> GetAllProductAsync() //Async Used by Unit Test { return await Task.FromResult(GetAllProducts()); // Task.FromResult to minimize extraneous code } public IHttpActionResult GetProduct(int id) { var product = products.FirstOrDefault((p) => p.Id == id); if (product == null) { return NotFound(); } return Ok(product); } public async Task<IHttpActionResult> GetProductAsync(int id) //Async Used by Unit Test { return await Task.FromResult(GetProduct(id)); } }
在Test中安装NuGet 包
右键Test project-> Manage NuGet Package -> install "Microsoft ASP.NET Web API 2 Core"
创建Tests
默认Test project下面有一个UnitTest1.cs,可以更改这个文件也可删除后重新添加。有一点需要注意的是,一定要是public class,不然运行不了。
本文中创建一个TestProductController.cs,该class中在[TestClass]标签下面,所有的方法都在[TestMethod]下面,run unit test的时候会自动运行TestClass类下面的TestMethod方法。
[TestClass] public class TestProductController { [TestMethod] public void GetAllProducts_ShouldReturnAllProducts() { var testProducts = GetTestProducts(); var controller = new ProductController(testProducts); var result = controller.GetAllProducts() as List<Product>; if (result != null) { Assert.AreEqual(testProducts.Count,result.Count); } } [TestMethod] public async Task GetAllProductsAsync_ShouldReturnAllProducts() { var testProducts = GetTestProducts(); var controller = new ProductController(testProducts); var result = await controller.GetAllProductAsync() as List<Product>; Assert.IsNotNull(result); Assert.AreEqual((object) testProducts.Count, result.Count); } [TestMethod] public void GetProduct_ShouldReturnCorrectProduct() { var testProducts = GetTestProducts(); var controller = new ProductController(testProducts); var result = controller.GetProduct(3) as OkNegotiatedContentResult<Product>; Assert.IsNotNull(result); Assert.AreEqual(testProducts[2].Name,result.Content.Name); } [TestMethod] public async Task GetProductAsync_ShouldReturnCorrectProduct() { var testProducts = GetTestProducts(); var controller = new ProductController(testProducts); var result = await controller.GetProductAsync(3) as OkNegotiatedContentResult<Product>; Assert.IsNotNull(result); Assert.AreEqual(testProducts[2].Name, result.Content.Name); } [TestMethod] public void GetProduct_ShouldNotFindProduct() { var controller = new ProductController(GetTestProducts()); var result = controller.GetProduct(999); Assert.IsInstanceOfType(result,typeof(NotFoundResult)); } private List<Product> GetTestProducts() { var testProducts = new List<Product> { new Product() {Id = 1, Name = "Demo1", Price = 1}, new Product() {Id = 2, Name = "Deom2", Price = 2.3M}, new Product() {Id = 3, Name = "Deom2", Price = 4.3M}, new Product() {Id = 4, Name = "Deom2", Price = 6.3M} }; return testProducts; } }
运行测试
在一再Test-> Run -> 运行指定test,所有test,查看覆盖率
- ASP.NET Web API 2 - 简单Unit Testing
- ASP.NET MVC + MVC Contrib + Unit Testing MVC 单元测试
- ASP.NET MVC4 WEB API简单应用
- asp.net web api
- ASP.NET Web API
- asp.net web api
- asp.net web api
- asp.net web api
- ASP.NET Web API(C#)实现简单增删改查
- 《Asp.Net Web API》-----webApi的简单使用
- 在ASP.NET Core MVC中构建简单 Web Api
- 入门 ASP.NET Web API 2 (C#)
- 《ASP.NET Web API 2框架揭秘》
- ASP.NET Web API 2框架揭秘
- ASP.NET Web API 2 入门教程
- 【翻译整理】ASP.NET Web API 2
- 入门--ASP.NET Web API 2
- Unit Testing
- jdk修改配置不鞥生效解决方案
- Linux命令行的艺术
- 根据URL的value找rowkey或者其他数据
- 安装VMware Workstation 10
- ibaits2中要注意remapResults-被坑了
- ASP.NET Web API 2 - 简单Unit Testing
- 3D rotate:
- js用POST方式页面跳转,避免在地址栏中显示传递参数
- Tomcat结构
- Android-系统换肤的几种方法
- 关于buffer overflow detected 程序崩溃的思考
- selection sort DEMO(linear complexity)
- gridview整页翻页的滑动,上滑和下滑,适应各种布局
- A2W、W2A、A2T、T2A、(USES_CONVERSION)