【问题标题】:How can i write unit test for this actionfilter我如何为这个 actionfilter 编写单元测试
【发布时间】:2016-01-24 16:57:17
【问题描述】:
public MyContext _db;
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
   if (_db == null || !_db.ChangeTracker.HasChanges())
   {
      return;
   }

   try
   {
      _db.SaveChanges();
   }
   catch
   {
   }
}

这是我的 wep api 项目的操作过滤器。 _db 上下文对象按每个请求注入此过滤器。我的意思是在服务层中完成所有处理后调用 SaveChanges() 方法一次。我的问题是如何测试这个过滤器?我如何模仿可能发生在任何控制器或服务层中的异常情况以及何时抛出异常时 saveChanges() 从未调用过?如何设置应用程序内部任何地方发生异常的情况?

【问题讨论】:

    标签: unit-testing asp.net-web-api2 moq xunit


    【解决方案1】:

    上周我一直在为我的 WebAPI 2 操作过滤器做同样的事情。

    我有一个操作过滤器来验证我的 ModelState,如果出现任何错误,它会抛出一个包含 200 HTTPcode 的错误列表。

    动作如下所示:

     public class ModelValidationActionFilterAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(HttpActionContext actionContext)
            {
                var modelState = actionContext.ModelState;
    
                if (!modelState.IsValid)
                {
                    actionContext.Response = ...
                }
            }
        }
    

    单元测试

    var httpControllerContext = new HttpControllerContext
                {
                    Request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/someUri")
                    {
                        Content = new ObjectContent(typeof(MyModel),
                            new MyModel(), new JsonMediaTypeFormatter())
                    },
                    RequestContext = new HttpRequestContext()
                };
    
                httpControllerContext.Request = new HttpRequestMessage();
                httpControllerContext.Request.SetConfiguration(new HttpConfiguration());
                var httpActionContext = new HttpActionContext { ControllerContext = httpControllerContext };
    
                var filter = new ModelValidationActionFilterAttribute();
    
                httpActionContext.ModelState.AddModelError("*", "Invalid model state");
    
                // act
                filter.OnActionExecuting(httpActionContext);
    
                // assert
                httpActionContext.Response.ShouldNotBe(null);
                httpActionContext.Response.ShouldBeOfType(typeof (HttpResponseMessage));
                var result = httpActionContext.Response.Content.ReadAsStringAsync().Result;
                BaseServiceResponse<object> resultResponse =
                    JsonConvert.DeserializeObject<BaseServiceResponse<object>>(result);
    
                resultResponse.Data.ShouldBe(null);
                resultResponse.Messages.Count.ShouldBe(1);
                resultResponse.Messages.First().Description.ShouldBe("Invalid model state");
    

    在您的情况下,您需要使用 IDbContext 接口模拟 DB 上下文 - 请参见此处:http://aikmeng.com/post/62817541825/how-to-mock-dbcontext-and-dbset-with-moq-for-unit

    【讨论】:

      【解决方案2】:

      如果在执行请求时发生未处理异常,则actionExecutedContext 上的Exception 属性将包含该异常。这是框架的一部分,而不是您需要测试的东西。在您的测试中,您可以简单地手动设置 Exception 属性并断言该属性采取了正确的操作。

      [Fact]
      public void Saves_data_on_failure()
      {
          var mockDbContext = new Mock<IDbContext>();
          var myAttribute = new MyAttribute(mockDbContext.Object);
      
          var executionContext = new HttpActionExecutedContext
          {
              Exception = new Exception("Request failed.")
          };
          myAttribute.OnActionExecuted(executionContext);
      
          mockDbContext.Verify(d => d.SaveChanges());
      }
      

      您可能还需要考虑是否要为所有类型的异常保存数据。数据可能处于无效/未知状态。

      【讨论】:

        猜你喜欢
        • 2018-03-13
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-17
        • 2012-01-06
        • 2019-03-17
        • 2016-04-02
        相关资源
        最近更新 更多