gpt4 book ai didi

c# - 集成测试 - 如何使用 Microsoft.Owin.Testing.TestServer 访问端点?

转载 作者:行者123 更新时间:2023-11-30 22:53:57 32 4
gpt4 key购买 nike

我已经设置了一个基本的 Web API 项目来测试编写集成测试,具有以下设置

启动:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(WebApiTest.Startup))]

namespace WebApiTest
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
WebApiConfig.Register(new System.Web.Http.HttpConfiguration());
}
}
}

以及以下 Controller :

using System.Web.Http;

namespace WebApiTest.Controllers
{
public class TestController : ApiController
{
public IHttpActionResult Post()
{
return Ok("Hello, world!");
}
}
}

对于我的测试项目,我使用 NUnit 进行测试,并且有两个类:一个抽象的 BaseTest 类和一个 ControllerTests 类其中包含一个测试,目的是将 Web 请求发送到上述 TestControllerGET 操作方法:

基础测试:

using Microsoft.Owin.Testing;
using NUnit.Framework;
using System;
using System.Net.Http;

namespace WebApiTest.Tests
{
public abstract class BaseTest : IDisposable
{
protected const string _url = "http://localhost/";
protected TestServer _server;

public void Dispose()
{
_server.Dispose();
}

[SetUp]
public void SetUp()
{
_server = TestServer.Create<Startup>();
}

protected HttpRequestMessage CreateRequest(string url, string
contentType, HttpMethod method)
{
HttpRequestMessage request = new HttpRequestMessage
{
RequestUri = new Uri(_url + url),
Method = method
};
request.Headers.Accept.Add(new
System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));
return request;
}
}
}

Controller 测试:

using FluentAssertions;
using NUnit.Framework;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

namespace WebApiTest.Tests
{
[TestFixture]
public class ControllerTests : BaseTest
{
[Test]
public async Task Test1()
{
HttpRequestMessage request = CreateRequest("test", "application/json", HttpMethod.Post);
HttpResponseMessage response = await _server.HttpClient.SendAsync(request);
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
}
}

我遇到的问题是我无法通过调用 await _server.HttpClient.SendAsync(request); 到达我的端点,因为我总是收到 404:未找到 响应。

我不完全确定我做错了什么,无论是未设置属性还是未映射的 Api 路由。

更新:这是我的WebApiConfig.Register()方法,供引用:

public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

最佳答案

Web API 配置不正确,因此它不知道路由

If you are self-hosting with OWIN, create a new HttpConfiguration instance. Perform any configuration on this instance, and then pass the instance to the Owin.UseWebApi extension method.

public class Startup {
public void Configuration(IAppBuilder app) {
var config = new System.Web.Http.HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config); //<-- THIS WAS MISSING
}
}

引用 Configuring Web API with OWIN Self-Hosting

关于c# - 集成测试 - 如何使用 Microsoft.Owin.Testing.TestServer 访问端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56675136/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com