[英]Load additional controller using TestServer in .NET Core 3.1 Web API
I'm using the following code in my unit tests:我在单元测试中使用以下代码:
var hostBuilder = new HostBuilder()
.ConfigureWebHost(webHost =>
{
// Add TestServer
webHost.UseTestServer();
webHost.UseStartup<Startup>();
});
I'd like to add a new controller only in the testserver.我只想在测试服务器中添加一个新的 controller。 Is this somehow possible using the same StartUp in my normal und unit test project?
在我的正常 und 单元测试项目中使用相同的 StartUp 是否有可能?
Update: To make it clear:更新:为了清楚起见:
I have my main controllers in MainProject.Controllers.我在 MainProject.Controllers 中有我的主要控制器。 When creating my TestServer in my UnitTestProject I want to additionally load UnitTestProject.Controllers (but only for the TestServer).
在我的 UnitTestProject 中创建我的 TestServer 时,我想另外加载 UnitTestProject.Controllers(但仅适用于 TestServer)。
If you want to add new controller, you can just create new file under ProjectName.Controller
namespace.如果要添加新的 controller,只需在
ProjectName.Controller
命名空间下创建新文件即可。
I think I found the answer:我想我找到了答案:
I've derived a TestStartup (in my UnitTestProject) class from Startup.我从 Startup 派生了一个 TestStartup(在我的 UnitTestProject 中)class。 This seems to do the trick.
这似乎可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.