我正在尝试使用NUnit设置测试来执行ASP.NET WebApi控制器的一些集成测试.我发现有几篇文章讨论使用HttpServer进行内存中托管,这似乎可以通过不需要托管所有内容的Web服务器来简化操作.
> ASP.NET Web API: in-memory hosting
> ASP.NET Web API integration testing with in-memory hosting
问题是我得到的唯一回应是404-Not Found.
通过浏览器或fiddler手动测试时,控制器正在工作.路径定义是从工作站点复制的. api项目由测试项目引用,并且dll将被复制到与测试相同的文件夹中.
提前致谢.
这是测试类
[TestFixture] public class InMemoryTests { private HttpServer Server; private string UrlBase = "http://some.server/"; [TestFixtureSetUp] public void Setup() { var config = new HttpConfiguration(); config.Routes.MapHttpRoute(name: "Default",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional }); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; Server = new HttpServer(config); } [Test] public void GetorderStatus() { var client = new HttpClient(Server); var request = createRequest("apI/Orders/GetorderStatus?companyCode=001&orderNumber=1234","application/json",HttpMethod.Get); using (HttpResponseMessage response = client.SendAsync(request).Result) { Assert.IsNotNull(response); Assert.AreEqual(HttpStatusCode.OK,response.StatusCode); Assert.NotNull(response.Content); } } private HttpRequestMessage createRequest(string url,string mthv,HttpMethod method) { var request = new HttpRequestMessage(); request.RequestUri = new Uri(UrlBase + url); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mthv)); request.Method = method; return request; } private HttpRequestMessage createRequest<T>(string url,HttpMethod method,T content,MediaTypeFormatter formatter) where T : class { HttpRequestMessage request = createRequest(url,mthv,method); request.Content = new ObjectContent<T>(content,formatter); return request; } public void dispose() { if (Server != null) { Server.dispose(); } } }
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。