更新时间:2023-02-17 07:49:15
这需要进行集成测试内存中的测试服务器,因为该属性在处理请求管道时由框架评估。
This would need integration testing with an in-memory test server because the attribute is evaluated by the framework as it processes the request pipeline.
集成测试可确保将应用程序的组件组装在一起时可以正常运行。 ASP.NET Core支持使用单元测试框架和内置的测试Web主机进行集成测试,该主机可以用于处理请求而无需网络开销。
Integration testing ensures that an application's components function correctly when assembled together. ASP.NET Core supports integration testing using unit test frameworks and a built-in test web host that can be used to handle requests without network overhead.
[Fact]
public async Task GetAsync_InvalidScope_ReturnsUnauthorizedResult() {
// Arrange
var server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
var client = server.CreateClient();
var url = "api/foo";
var expected = HttpStatusCode.Unauthorized;
// Act
var response = await client.GetAsync(url);
// Assert
Assert.AreEqual(expected, response.StatusCode);
}
您还可以创建一个专门用于测试的启动,它将替换所有依赖项如果您不希望测试打到实际的生产实现中,则可以使用存根/模拟的DI来实现。
You can also create a start up specifically for the test that will replace any dependencies for DI with stubs/mocks if you do not want the test hitting actual production implementations.