之前如果对 ASP.NET WebAPI 进行单元测试(HttpClient 发起请求,并且可调试 WebAPI),一般采用 Owin 的方式,具体参考:《开发笔记:用 Owin Host 实现脱离 IIS 跑 Web API 单元测试》
示例代码:
public class ValuesWebApiTest : IDisposable
{
private const string HOST_ADDRESS = "http://localhost:8001";
private IDisposable _webApp;
private HttpClient _httClient;
public AdTextUnitWebApiTest()
{
_webApp = WebApp.Start<Startup>(HOST_ADDRESS);
Console.WriteLine("Web API started!");
_httClient = new HttpClient();
_httClient.BaseAddress = new Uri(HOST_ADDRESS);
Console.WriteLine("HttpClient started!");
}
[Fact]
public async Task Get()
{
var response = await _httClient.GetAsync("/api/values");
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine(response.StatusCode);
Console.WriteLine((await response.Content.ReadAsAsync<HttpError>()).ExceptionMessage);
}
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var test = await response.Content.ReadAsStringAsync();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
public void Dispose()
{
_httClient.Dispose();
_webApp.Dispose();
}
}
本来想在 ASP.NET 5 WebAPI 项目中,也用这一套测试代码,但发现并不适用,因为 ASP.NET WebAPI 2 和 ASP.NET 5 WebAPI 并不是特别一样,比如 Startup.cs 的配置等等,之前使用 WebApp.Start<Startup>(HOST_ADDRESS)
的方式启动 WebAPI 项目,而 ASP.NET 5 WebAPI 变成了这样的:
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
想用 WebApplication.Run
的方式替换掉 WebApp.Start
,但发现并不可行,比如 args 的参数问题,自己想的有点简单了,后来 Google 搜索了一些资料,发现 ASP.NET 5 增加了 TestServer,自己找资料配置了很久,看别人的示例代码很简单,但我运行的时候就是各种报错,主要原因是程序包的版本不对,因为我是按照 project.json 的提示安装的,比如 Microsoft.AspNet.TestHost
这个程序包,提示最新版本为 1.0.0-rc2-15960
,并且没有 1.0.0-rc1-final
版本,然后我就安装提示安装的 rc2,就报下面的异常:
异常信息:Could not load type 'Microsoft.AspNet.Builder.RequestDelegate' from assembly 'Microsoft.AspNet.Http.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
根据提示,我以为异常原因是没有加载 Microsoft.AspNet.Http.Abstractions
程序集,然后又添加此程序集,重新运行发现还是报错。。。后面具体的过程就不记录了,反正坑很大,根本原因是 Microsoft.AspNet.TestHost
程序包的版本不对,应该安装 1.0.0-rc1-final
版本,我是后来无意间重启 VS2015 发现的。
下面贴一下 ASP.NET 5 进行单元测试的一些代码。
首先 ASP.NET 5 WebAPI 项目 Startup.cs 配置代码:
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Demo.WebApi
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc();
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
ValuesWebApiTest 测试代码:
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Demo.WebApiTests
{
public class ValuesWebApiTest
{
public TestServer _server;
public ValuesWebApiTest()
{
_server = TestServer.Create(app =>
{
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
var loggerFactory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
new CNBlogs.Ad.WebApi.Startup(env).Configure(app, env, loggerFactory);
}, services =>
{
services.AddMvc();
services.Configure();
});
}
}
[Fact]
public async Task Get()
{
var response = await _server.CreateClient().GetAsync("/api/values");
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine(response.StatusCode);
Console.WriteLine((await response.Content.ReadAsAsync<HttpError>()).ExceptionMessage);
}
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var test = await response.Content.ReadAsStringAsync();
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
project.json 配置代码:
{
"frameworks": {
"dnx451": { }
},
"dependencies": {
"Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-rc1-final",
"Microsoft.Net.Http": "2.2.29",
"Microsoft.AspNet.TestHost": "1.0.0-rc1-final",
"xunit": "2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-build204"
},
"commands": {
"test": "xunit.runner.dnx"
}
}
运行测试成功,并且可以 Debug 调试,需要注意 using 引用(没用的我都去掉了),还有程序包的版本号。
注:如果 VS2015 Test Explorer 中找不到测试示例,需要安装最新的 xUnit 程序包。
"xunit": "2.2.0-beta2-build3256",
"xunit.runner.dnx": "2.1.0-rc2-build209"
xUnit 程序包地址:http://myget.org/gallery/xunit
本文转自田园里的蟋蟀博客园博客,原文链接:http://www.cnblogs.com/xishuai/p/aspnet5-testserver-unit-test.html,如需转载请自行联系原作者