How does the In-Memory HttpServer know which WebAPI project to host?

前端 未结 3 900
一生所求
一生所求 2020-12-10 12:55

I want to run tests against WebAPI project using a popular in-memory hosting strategy.

My tests reside in a separate project.

Here\'s the start of my test

3条回答
  •  眼角桃花
    2020-12-10 13:37

    Just make sure that you are invoking any controller from the web api project into your separate project to ensure that web api project is loaded in in-memory.Example:

    [TestMethod]
    public void TestMethod1()
    {
    
     //// So that the web api project is loaded in-memory
     {webapi Project name}.Controller.{controllerName} = new {controller name}() ;
        HttpConfiguration config = new HttpConfiguration();
    
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {id = RouteParameter.Optional});
    
        HttpServer server = new HttpServer(config);
        HttpMessageInvoker client = new HttpMessageInvoker(server)
    }
    

提交回复
热议问题