在内存web api服务器中创建时,路由无法正确映射

时间:2016-10-21 11:31:09

标签: c# unit-testing asp.net-web-api

当我卷曲到/test路线时它工作正常,但是当试图在相同路线上击中内存服务器时,测试下面是404。

检查_client_config似乎没问题时 - 虽然我不确定如何确认我的内存服务器是否正常运行。

有没有人知道如何让我的内存网络服务器正确映射它的路由所以我的测试方法可以达到它?

namespace Robo.Tests.Controllers
{

    [TestClass]
    public class IntegrationTests
    {
        private HttpMessageInvoker _client;
        private HttpConfiguration _config = new HttpConfiguration();

        [TestInitialize]
        public void SetupTest()
        {
            _config.MapHttpAttributeRoutes();
            _config.EnsureInitialized();

            var server = new HttpServer(_config);
            _client = new HttpMessageInvoker(server);
        }

        [TestMethod]
        public async Task Test()
        {
            var result = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "http://localhost/test"), CancellationToken.None);
        }
    }
}

和控制器以防您感兴趣

namespace Robo.Controllers
{
    //[ValidationActionFilter]
    public class CVController : ApiController
    {

        [HttpGet]
        [Route("test")]
        public async Task<IHttpActionResult> test()
        {
            return Ok();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

对于内存服务器测试已创建以下实用程序类。它基本上包含了所示示例中的设置功能。<​​/ p>

internal interface IHttpTestServer : IDisposable {
    HttpConfiguration Configuration { get; }
    HttpClient CreateClient();
}

internal class HttpTestServer : IHttpTestServer {
    HttpServer httpServer;

    public HttpTestServer(HttpConfiguration configuration = null) {
        httpServer = new HttpServer(configuration ?? new HttpConfiguration());
    }

    public HttpConfiguration Configuration {
        get { return httpServer.Configuration; }
    }

    public HttpClient CreateClient() {
        var client = new HttpClient(httpServer);
        return client;
    }

    public void Dispose() {
        if (httpServer != null) {
            httpServer.Dispose();
            httpServer = null;
        }
    }

    public static IHttpTestServer Create(HttpConfiguration configuration = null) {
        return new HttpTestServer(configuration);
    }
}

以下测试旨在演示使用OP

在内存服务器中的使用
[TestClass]
public class IntegrationTests {

    [TestMethod]
    public async Task Test() {


        using (var server = HttpTestServer.Create()) {
            //Arrange
            var config = server.Configuration;
            config.MapHttpAttributeRoutes();
            config.EnsureInitialized();

            var client = server.CreateClient();
            var url = "http://localhost/test";
            var request = new HttpRequestMessage(HttpMethod.Get, url);
            var expected = System.Net.HttpStatusCode.OK;

            //Act
            var result = await client.SendAsync(request, CancellationToken.None);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result.StatusCode);
        }

    }

    public class CVController : ApiController {
        [HttpGet]
        [Route("test")]
        public async Task<IHttpActionResult> test() {
            return Ok();
        }
    }
}

测试通行证。

关于这个例子的事情是测试和控制器存在于同一个程序集中,因此map属性扫描它所调用的程序集,并找到带有属性路径的API控制器。如果控制器存在于另一个项目中,那么应该在HttpConfiguration上调用该项目的Web API配置以正确配置Web API。

<强>更新

测试项目和web api项目应该是两个独立的项目。也就是说,Web项目应该有一个带有静态WebApiConfig.cs类和方法的WebApiConfig.Register文件。该方法采用HttpConfiguration参数。测试应该使用该方法在内存调用中配置api。

[TestClass]
public class IntegrationTests {

    [TestMethod]
    public async Task Test() {


        using (var server = HttpTestServer.Create()) {
            //Arrange
            var config = server.Configuration;
            //Config server
            MyWebApiNamespace.WebApiConfig.Register(config);

            var client = server.CreateClient();
            var url = "http://localhost/test";
            var request = new HttpRequestMessage(HttpMethod.Get, url);
            var expected = System.Net.HttpStatusCode.OK;

            //Act
            var result = await client.SendAsync(request, CancellationToken.None);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(expected, result.StatusCode);
        }
    }
}