我想进行集成测试(使用WebApplicationFactory
来测试我的路由配置和序列化层。
我希望测试(大致)像这样:
var controllerSpy = new BooksControllerSpy(201);
var factory = new WebApplicationFactory<Startup>();
var client = factory.WithWebHostBuilder(builder =>
{
// ???? inject controllerSpy into controller registry
}).CreateClient();
var response = await client.PostAsync(
"/api/books",
new StringContent(@"{ ""name"": ""The Bible"" }")
);
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.Equal("The Bible", controllerSpy.DeserializedCommand.Name);
我特别想知道如何将控制器注入测试服务器的控制器注册表中,以便将测试重点放在路由和序列化上。
提前感谢您的任何想法!