Unit Testing / Integration Testing Web API with HttpClient in Visual Studio 2013

后端 未结 1 1012
长发绾君心
长发绾君心 2020-12-02 17:46

I am having a hard time trying to test my API controller with Visual Studio 2013. My one solution has a Web API Project and a Test project. In my test project, I have a Uni

相关标签:
1条回答
  • 2020-12-02 18:22

    Referencing the following article I was able to do...

    ASP.NET Web API integration testing with in-memory hosting

    by working with a HttpServer and a HttpConfiguration passed into HttpClient. In the following example I created a simple ApiController that uses attribute routing. I configured the HttpConfiguration to map attribute routes and then passed it to the new HttpServer. The HttpClient can then use the configured server to make integration test calls to the test server.

    public partial class MiscUnitTests {
        [TestClass]
        public class HttpClientIntegrationTests : MiscUnitTests {
    
            [TestMethod]
            public async Task HttpClient_Should_Get_OKStatus_From_Products_Using_InMemory_Hosting() {
    
                var config = new HttpConfiguration();
                //configure web api
                config.MapHttpAttributeRoutes();
    
                using (var server = new HttpServer(config)) {
    
                    var client = new HttpClient(server);
    
                    string url = "http://localhost/api/product/hello/";
    
                    var request = new HttpRequestMessage {
                        RequestUri = new Uri(url),
                        Method = HttpMethod.Get
                    };
    
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    using (var response = await client.SendAsync(request)) {
                        Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                    }
                }
            }
        }
    
        public class ProductController : ApiController {
            [HttpGet]
            [Route("api/product/hello/")]
            public IHttpActionResult Hello() {
                return Ok();
            }
        }
    }
    

    There was no need to have another instance of VS running in order to integration test the controller.

    The following simplified version of the test worked as well

    var config = new HttpConfiguration();
    //configure web api
    config.MapHttpAttributeRoutes();
    
    using (var server = new HttpServer(config)) {
    
        var client = new HttpClient(server);
    
        string url = "http://localhost/api/product/hello/";
    
        using (var response = await client.GetAsync(url)) {
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
    }
    

    In your case you need to make sure that you are configuring the server properly to match your web api setup. This would mean that you have to register your api routes with the HttpConfiguration object.

    var config = new HttpConfiguration();
    //configure web api
    WebApiConfig.Register(config);
    //...other code removed for brevity
    
    0 讨论(0)
提交回复
热议问题