2014-10-28 95 views
1

我试图在采用ODataQueryOptions参数的Web API控制器方法中对GET方法进行单元测试。我需要验证OData过滤器的结果,我不知道如何做断言。我坚持什么?我正确地测试这个吗?使用Moq在Web API中进行单元测试OData

我用这作为灵感:Web API, OData, $inlinecount and testing

编辑:我需要坚持这一点对于一个有效的测试?这是我的TryGetContentValue调用返回的内容。如果是这样,我该怎么做?

{System.Collections.Generic.List`1[CSR.Service.Models.CSRRole].Where($it => ($it.RoleName == value(System.Web.Http.OData.Query.Expressions.LinqParameterContainer+TypedLinqParameterContainer`1[System.String]).TypedProperty))} 

单位测试

[TestMethod] 
     public void GetTestWithOData() 
     { 
      // Arrange 
      var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:21584/api/test?$filter=RoleName%20eq%20'User'"); 
      ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); 
      modelBuilder.EntitySet<CSRRole>("CSRRoles"); 
      var opts = new ODataQueryOptions<CSRRole>(new ODataQueryContext(modelBuilder.GetEdmModel(), typeof(CSRRole)), request); 
      var uowMock = new Mock<ICSRUnitOfWork>(); 
      uowMock.SetupGet(i => i.TestRepository).Returns(new Mock<IGenericRepository<CSRRole, CSRContext>>().Object); 
      var controller = new TestController(uowMock.Object); 
      controller.Request = new HttpRequestMessage(); 
      controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); 

      // Act 
      var result = controller.Get(opts); 

      //Assert 
      IQueryable<CSRRole> roles; 
      Assert.IsNotNull(result);     
      // **** I don't think this is a good test **** 
      Assert.IsTrue(result.TryGetContentValue<IQueryable<CSRRole>>(out roles)); 
     } 

控制器方法

public HttpResponseMessage Get(System.Web.Http.OData.Query.ODataQueryOptions<CSRRole> options) 
     { 
      HttpResponseMessage response; 

      if (options == null) 
      { 
       return response = new HttpResponseMessage(HttpStatusCode.BadRequest); 
      } 

      var result = options.ApplyTo(_csrUnitOfWork.TestRepository.Get()); 

      if (result == null) 
      { 
       response = new HttpResponseMessage(HttpStatusCode.NotFound); 
      } 
      else 
      { 
       response = Request.CreateResponse(HttpStatusCode.OK, result); 
       response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300)); 
      } 
      return response; 
     } 
+0

你是否以某种方式工作? – Rikard 2015-05-20 07:15:24

+0

@Rikard ......我从来没有放弃过。如果您有解决方案,请发布答案。 – 2015-05-20 10:56:56

+0

我管理测试查询,如果我使用QueryOptions.ApplyTo。你应该使用那个函数来代替吗? – Rikard 2015-05-20 13:19:07

回答

0

您需要模拟opts了。例如:

var opts = new Mock<IDataQueryOptions>(); 
opts.Setup(m => m.ApplyTo(It.IsAny</* Whatever class this takes in */>())) 
    .Returns(/* Whatever this needs to return */); 

注意嘲笑作用于一个接口,所以你也需要创建它。用一个嘲弄的对象,你打电话告诉它返回你需要的单元测试。

+0

什么是IDataQueryOptions?如果你的意思是IODataQueryOptions,它不存在。没有我知道的ODataQueryOptions的接口。 – 2014-10-28 18:43:10

+0

@BigDaddy我认为他们说你需要创建'IDataQueryOptions'(和一个包装类)来抽象使用'ODataQueryOptions'。 – 2014-10-29 13:16:22

+1

@PatrickQuirk ......你可能是对的,但有一个更好的方法来测试这个。顺便说一句,ODataQueryOptions似乎不是瓶颈。它主张它返回的是我的问题。 – 2014-10-29 15:07:13