您的位置:首页 > 编程语言 > ASP

ASP.NET Web API 2 - 简单Unit Testing

2015-07-09 08:43 706 查看
资源: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,查看覆盖率




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息