我们如何在Asp.Net核心中对测试动作签名级别的属性进行单元化?

时间:2019-08-01 13:42:28

标签: asp.net-core

当客户端向WebAPI请求URL时,asp.net核心框架确保在url中传递的参数成为端点的请求参数,我们如何测试在查询中传递的参数是否正确映射到了参数?

如果在端点之一而不是[FromQuery]中,开发人员使用了[FromRoute],则来自URL的参数将不会达到端点参数。如果参数是可选过滤器,我们只能在端到端自动化过程中识别此类问题。我们希望找到一种可以在诸如单元测试之类的简单测试中找到的方法。

public class MyController
{
  //Right code
  public Response MyGet([FromQuery] MyParamModel param)
  {
      return MyManager.GetData(param.id,param.name);
  }
  //Wrong Code the developer used a wrong attribute here, instead of FromQuery 
  //to FromRoute 
  //If the parameters are optional the GetData would still work and return
  //Wrong results. How do we find such errors in simpler tests like Unit Test? 
  public Response MyGet([FromRoute] MyParamModel param)
  {
      return MyManager.GetData(param.id,param.name);
  }
}

0 个答案:

没有答案