1

摘要

  • 我正在尝试使用ActionFilter实现测试控制器
  • 单元测试失败,因为在单元测试中没有调用 ActionFilter。
  • 通过 Postman 进行的测试按预期工作,并获得了正确的结果。
  • 控制器可以像这样测试还是应该转移到集成测试?

细分

我可以在单元测试中自行测试 ActionFilter,我想做的是在单元测试中测试控制器。

动作过滤器如下所示:

 public class ValidateEntityExistAttribute<T> : IActionFilter
        where T : class, IEntityBase
    {
        readonly AppDbContext _appDbContext;
        public ValidateEntityExistAttribute(AppDbContext appDbContext)
        {
            this._appDbContext = appDbContext;
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {}

        public void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ActionArguments.ContainsKey("id"))
            {
                context.Result = new BadRequestObjectResult("The id must be passed as parameter");
                return;
            }

            int id = (int)context.ActionArguments["id"];

            var foundEntity = _appDbContext.Set<T>().Find(id);
            if (foundEntity == null)
                context.Result = new NotFoundResult();
            else
                context.HttpContext.Items.Add("entity_found", foundEntity);
        }
    }

在启动文件中将 ActionFilter 实现添加到服务中

ConfigureServices(IServiceCollection services)
{
...
 services.AddScoped<ValidateEntityExistAttribute<Meeting>>();
...
}

过滤器可以应用于任何需要检查实体是否存在的控制器方法。即GetById 方法。

[HttpGet("{id}")]
[ServiceFilter(typeof(ValidateEntityExistAttribute<Meeting>))]
public async Task<ActionResult<MeetingDto>> GetById(int id)
    {            
        var entity = HttpContext.Items["entity_found"] as Meeting;
        await Task.CompletedTask;
        return Ok(entity.ConvertTo<MeetingDto>());
    }

在 xUnit 测试中,我设置了测试来测试控制器,如下所示:

[Fact]
public async Task Get_Meeting_Record_By_Id()
{
    // Arrange
    var _AppDbContext = AppDbContextMocker.GetAppDbContext(nameof(Get_All_Meeting_Records));
    var _controller = InitializeController(_AppDbContext);

    //Act
    
    var all = await _controller.GetById(1);

    //Assert
    Assert.Equal(1, all.Value.Id);

    //clean up otherwise the other test will complain about key tracking.
    await _AppDbContext.DisposeAsync();
}

这就是 InitializeController 方法的样子,我留下了注释行,以便它对我所尝试的内容可见,注释代码都不起作用。我嘲笑并使用了默认类。

private MeetingController InitializeController(AppDbContext appDbContext)
    {

        var _controller = new MeetingController(appDbContext);

        var spf = new DefaultServiceProviderFactory(new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
        var sc = spf.CreateBuilder(new ServiceCollection());

        sc.AddMvc();
        sc.AddControllers();
        //(config =>
        //{
        //    config.Filters.Add(new ValidateModelStateAttribute());
        //    config.Filters.Add(new ValidateEntityExistAttribute<Meeting>(appDbContext));
        //});
        
        sc.AddTransient<ValidateModelStateAttribute>();
        sc.AddTransient<ValidateEntityExistAttribute<Meeting>>();

        var sp = sc.BuildServiceProvider();

        //var mockHttpContext = new Mock<HttpContext>();
        var httpContext = new DefaultHttpContext
        {
            RequestServices = sp
        };
        //mockHttpContext.Setup(cx => cx.RequestServices).Returns(sp);

        //var contDesc = new ControllerActionDescriptor();
        //var context = new ControllerContext();
        //var context = new ControllerContext(new ActionContext(mockHttpContext.Object, new RouteData(), contDesc));

        //context.HttpContext = mockHttpContext.Object;
        //context.HttpContext = httpContext;
        //_controller.ControllerContext = context;
        _controller.ControllerContext.HttpContext = httpContext;

        return _controller;
    }

我遇到的问题是,在运行单元测试时,永远不会调用 ActionFilter 实现,从而破坏了测试,因为 var entity = HttpContext.Items["entity_found"] as Meeting;在控制器中始终为null!更准确地说HttpContext.Items,始终为空。

ActionFilter 中的断点永远不会被击中。

当通过邮递员进行测试时,一切都按预期工作,并且断点被击中

有没有办法以这种方式将控制器作为单元测试进行测试,或者这个测试现在应该转移到集成

4

1 回答 1

0

感谢@Fei Han 提供有关单元测试控制器的链接。

事实证明这是设计使然,如微软文档中所述

单元测试控制器 设置控制器动作的单元测试以 关注控制器的行为。控制器单元测试避免了 过滤器、路由和模型绑定等场景。 涵盖共同响应请求的组件之间的交互的 测试由集成测试处理。

所以测试应该转向集成测试。

这个特殊的场景中,方法没有详细的实现,对它进行单元测试GetById(int id)几乎没有任何价值。

如果该GetById(int id)方法具有更复杂的实现,或者如果ActionFilter没有阻止进一步处理,则HttpContext.Items["entity_found"]应该模拟。

于 2020-07-09T07:50:34.183 回答