I want to run tests against WebAPI project using a popular in-memory hosting strategy.
我想使用流行的内存托管策略对WebAPI项目运行测试。
My tests reside in a separate project.
我的测试位于一个单独的项目中。
Here's the start of my test
这是我考试的开始
[TestMethod]
public void TestMethod1()
{
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)
}
The client is initialized with the HttpServer, establishing the direct client-server connection.
使用HttpServer初始化客户端,建立直接的客户端 - 服务器连接。
Other than providing route config info, how does HttpServer know which WebAPI project to host?
除了提供路由配置信息之外,HttpServer如何知道要托管哪个WebAPI项目?
How to host multiple WebAPI projects at the same time?
如何同时托管多个WebAPI项目?
Seems HttpServer does some magic to locate WebAPI projects?
似乎HttpServer能够找到WebAPI项目吗?
Thanks
谢谢
3 个解决方案
#1
14
Web API depends on a service called IAssembliesResolver
to get all the assemblies and scans them to find controllers which implement the IHttpController
interface.
Web API依赖于名为IAssembliesResolver的服务来获取所有程序集并扫描它们以查找实现IHttpController接口的控制器。
Now sometimes Web API might be unable to find your controller depending on whether the assembly has been loaded into the current app domain or not. In that scenario you would need to make sure that your assembly is loaded.
现在,有时Web API可能无法找到您的控制器,具体取决于程序集是否已加载到当前应用程序域中。在这种情况下,您需要确保已加载程序集。
Looking at your sample test code, it appears that you are not referring to any type from your Web API project in which case i assume the Web API project's assembly would not be loaded.
查看示例测试代码,看起来您没有引用Web API项目中的任何类型,在这种情况下,我假设不会加载Web API项目的程序集。
Also you seem to be registering the routes again in your test. I would suggest to use the WebApiConfig.Register(HttpConfiguration)
of your Web API project to do all the registration stuff. This way you would be testing with the same settings that your Web API project has.
此外,您似乎在测试中再次注册路线。我建议使用Web API项目的WebApiConfig.Register(HttpConfiguration)来完成所有注册工作。这样,您将使用与Web API项目相同的设置进行测试。
Notes:
笔记:
-
When running tests using in-memory server, your requests/responses wouldn't go through the formatters' serialization/deserialization process which is dangerous as you could be having real issues during them. So you would need to make sure to take care about this. Long time back i wrote a blog post regarding this. You can check it out here.
当使用内存服务器运行测试时,您的请求/响应将不会通过格式化程序的序列化/反序列化过程,这是危险的,因为您可能在它们期间遇到实际问题。所以你需要确保照顾这个。很久以前我写了一篇关于此的博文。你可以在这里查看。
-
Fiddler tool is very useful in looking at the raw requests/responses to diagnose any issues. You would be loosing this ability if you are doing in-memory testing though.
Fiddler工具在查看原始请求/响应以诊断任何问题时非常有用。如果您正在进行内存测试,那么您将失去此功能。
#2
1
Web Api should find all your controllers that inherit from ApiController. As long as all your controllers are in the same solution it should work just fine. I have a very similar setup that runs tests using the in-memory httpserver against controllers in another project. This gives me the ability to do very fast "integration" tests in my unit test project.
Web Api应该找到从ApiController继承的所有控制器。只要所有控制器都在同一个解决方案中,它应该可以正常工作。我有一个非常类似的设置,使用内存中的httpserver对另一个项目中的控制器运行测试。这使我能够在单元测试项目中进行非常快速的“集成”测试。
#3
0
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:
只需确保从web api项目调用任何控制器到单独的项目中,以确保web api项目加载到内存中。例如:
[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)
}
#1
14
Web API depends on a service called IAssembliesResolver
to get all the assemblies and scans them to find controllers which implement the IHttpController
interface.
Web API依赖于名为IAssembliesResolver的服务来获取所有程序集并扫描它们以查找实现IHttpController接口的控制器。
Now sometimes Web API might be unable to find your controller depending on whether the assembly has been loaded into the current app domain or not. In that scenario you would need to make sure that your assembly is loaded.
现在,有时Web API可能无法找到您的控制器,具体取决于程序集是否已加载到当前应用程序域中。在这种情况下,您需要确保已加载程序集。
Looking at your sample test code, it appears that you are not referring to any type from your Web API project in which case i assume the Web API project's assembly would not be loaded.
查看示例测试代码,看起来您没有引用Web API项目中的任何类型,在这种情况下,我假设不会加载Web API项目的程序集。
Also you seem to be registering the routes again in your test. I would suggest to use the WebApiConfig.Register(HttpConfiguration)
of your Web API project to do all the registration stuff. This way you would be testing with the same settings that your Web API project has.
此外,您似乎在测试中再次注册路线。我建议使用Web API项目的WebApiConfig.Register(HttpConfiguration)来完成所有注册工作。这样,您将使用与Web API项目相同的设置进行测试。
Notes:
笔记:
-
When running tests using in-memory server, your requests/responses wouldn't go through the formatters' serialization/deserialization process which is dangerous as you could be having real issues during them. So you would need to make sure to take care about this. Long time back i wrote a blog post regarding this. You can check it out here.
当使用内存服务器运行测试时,您的请求/响应将不会通过格式化程序的序列化/反序列化过程,这是危险的,因为您可能在它们期间遇到实际问题。所以你需要确保照顾这个。很久以前我写了一篇关于此的博文。你可以在这里查看。
-
Fiddler tool is very useful in looking at the raw requests/responses to diagnose any issues. You would be loosing this ability if you are doing in-memory testing though.
Fiddler工具在查看原始请求/响应以诊断任何问题时非常有用。如果您正在进行内存测试,那么您将失去此功能。
#2
1
Web Api should find all your controllers that inherit from ApiController. As long as all your controllers are in the same solution it should work just fine. I have a very similar setup that runs tests using the in-memory httpserver against controllers in another project. This gives me the ability to do very fast "integration" tests in my unit test project.
Web Api应该找到从ApiController继承的所有控制器。只要所有控制器都在同一个解决方案中,它应该可以正常工作。我有一个非常类似的设置,使用内存中的httpserver对另一个项目中的控制器运行测试。这使我能够在单元测试项目中进行非常快速的“集成”测试。
#3
0
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:
只需确保从web api项目调用任何控制器到单独的项目中,以确保web api项目加载到内存中。例如:
[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)
}