Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core mvc 如何在asp.net core中对web api的操作筛选器属性进行单元测试?_Asp.net Core Mvc_Actionfilterattribute - Fatal编程技术网

Asp.net core mvc 如何在asp.net core中对web api的操作筛选器属性进行单元测试?

Asp.net core mvc 如何在asp.net core中对web api的操作筛选器属性进行单元测试?,asp.net-core-mvc,actionfilterattribute,Asp.net Core Mvc,Actionfilterattribute,我已经为web api编写了一个操作过滤器。如果api控制器中的某个方法引发未处理的异常,则过滤器将创建一个内部错误500响应 我需要知道如何测试过滤器 我进行了广泛的研究,但无法创建一个合适的测试。我尝试了上下文模拟、服务定位器实现,甚至使用测试服务器进行了集成测试 web api控制器如下所示: namespace Plod.Api.ApiControllers { [TypeFilter(typeof(UnhandledErrorFilterAttribute))] [Ro

我已经为web api编写了一个操作过滤器。如果api控制器中的某个方法引发未处理的异常,则过滤器将创建一个内部错误500响应

我需要知道如何测试过滤器

我进行了广泛的研究,但无法创建一个合适的测试。我尝试了上下文模拟、服务定位器实现,甚至使用测试服务器进行了集成测试

web api控制器如下所示:

namespace Plod.Api.ApiControllers
{
    [TypeFilter(typeof(UnhandledErrorFilterAttribute))]
    [Route("api/[controller]")]
    public class GamesController : BaseApiController
    {
        public GamesController(IGameService repository, 
            ILogger<GamesController> logger,
            IGameFactory gameFactory
            ) : base(
                repository, 
                logger,
                gameFactory
                )
        { }

        // ..... controller methods are here
    }
}

我甚至欢迎对过滤器实现进行更改,作为一种可能的解决方法。任何帮助或想法都将不胜感激。谢谢。

你可能不能。然而,您可以做的是启动一个,然后用一个HttpClient来实现它。这实际上是一个集成测试,而不是单元测试。然而,它是一种很好的集成测试,因为它可以在管道中安全运行

本文档说明了如何执行此操作:

您将面临的问题是,您需要模拟应用程序中的底层服务。如果您不这样做,您的整个服务器将启动并尝试访问数据库等。下面是一个示例。这是使用最小起订量。顺便提一下,我正在与单元测试共享
ConfigureServices
方法,以便它们使用模拟服务的相同对象网格。您仍然可以使用Moq或NSubstitute的完整功能来测试后端(甚至前端)

我可以在测试中使用断点命中我的属性。

private void ConfigureServices(IServiceCollection services)
{
    var hostBuilder = new WebHostBuilder();
    hostBuilder.UseStartup<TestStartup>();
    hostBuilder.ConfigureServices(services =>
    {
        ConfigureServices(services);
    });

    _testServer = new TestServer(hostBuilder);
    _httpClient = _testServer.CreateClient();
}


private void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(_storageManagerFactory.Object);
    services.AddSingleton(_blobReferenceManagerMock.Object);
    services.AddSingleton(_ipActivitiesLoggerMocker.Object);
    services.AddSingleton(_loggerFactoryMock.Object);
    services.AddSingleton(_hashingService);
    services.AddSingleton(_settingsServiceMock.Object);
    services.AddSingleton(_ipActivitiesManager.Object);
    services.AddSingleton(_restClientMock.Object);
    _serviceProvider = services.BuildServiceProvider();
}


public class TestStartup 
{
    public void Configure(
        IApplicationBuilder app,
        ISettingsService settingsService)
    {
        app.Configure(settingsService.GetSettings());
    }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        var mvc = services.AddMvc(option => option.EnableEndpointRouting = false);
        mvc.AddApplicationPart(typeof(BlobController).Assembly);
        services.AddSingleton(new Mock<IHttpContextAccessor>().Object);
        return services.BuildServiceProvider();
    }
}
专用void配置服务(IServiceCollection服务)
{
var hostBuilder=新的WebHostBuilder();
hostBuilder.UseStartup();
hostBuilder.ConfigureServices(服务=>
{
配置服务(服务);
});
_testServer=新的testServer(hostBuilder);
_httpClient=_testServer.CreateClient();
}
专用void配置服务(IServiceCollection服务)
{
services.AddSingleton(_storageManagerFactory.Object);
services.AddSingleton(_blobReferenceManagerMock.Object);
services.AddSingleton(_ipActivitiesLoggerMocker.Object);
services.AddSingleton(_loggerFactoryMock.Object);
AddSingleton(_hashingService);
services.AddSingleton(_settingsServiceMock.Object);
services.AddSingleton(_ipactiviteManager.Object);
services.AddSingleton(_restClientMock.Object);
_serviceProvider=services.BuildServiceProvider();
}
公共类测试启动
{
公共无效配置(
IApplicationBuilder应用程序,
i设置服务设置(服务)
{
app.Configure(setingsService.GetSettings());
}
公共IServiceProvider配置服务(IServiceCollection服务)
{
var mvc=services.AddMvc(option=>option.EnableEndpointRouting=false);
mvc.AddApplicationPart(typeof(BlobController.Assembly));
services.AddSingleton(新的Mock().Object);
return services.BuildServiceProvider();
}
}

->太棒了!我确实怀疑单元测试是不合适的,但这个答案证实了这一点。
private void ConfigureServices(IServiceCollection services)
{
    var hostBuilder = new WebHostBuilder();
    hostBuilder.UseStartup<TestStartup>();
    hostBuilder.ConfigureServices(services =>
    {
        ConfigureServices(services);
    });

    _testServer = new TestServer(hostBuilder);
    _httpClient = _testServer.CreateClient();
}


private void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(_storageManagerFactory.Object);
    services.AddSingleton(_blobReferenceManagerMock.Object);
    services.AddSingleton(_ipActivitiesLoggerMocker.Object);
    services.AddSingleton(_loggerFactoryMock.Object);
    services.AddSingleton(_hashingService);
    services.AddSingleton(_settingsServiceMock.Object);
    services.AddSingleton(_ipActivitiesManager.Object);
    services.AddSingleton(_restClientMock.Object);
    _serviceProvider = services.BuildServiceProvider();
}


public class TestStartup 
{
    public void Configure(
        IApplicationBuilder app,
        ISettingsService settingsService)
    {
        app.Configure(settingsService.GetSettings());
    }

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        var mvc = services.AddMvc(option => option.EnableEndpointRouting = false);
        mvc.AddApplicationPart(typeof(BlobController).Assembly);
        services.AddSingleton(new Mock<IHttpContextAccessor>().Object);
        return services.BuildServiceProvider();
    }
}