2014-10-28 2 views
1

ODataQueryOptions 매개 변수를 사용하는 Web API 컨트롤러 메서드에서 GET 메서드를 단위 테스트하려고합니다. OData 필터 결과를 확인해야하며 어설 션을 수행하는 방법을 모르겠습니다. 내가 무엇을 주장합니까? 이걸 제대로 테스트하고 있습니까? Web API, OData, $inlinecount and testingMoq를 사용하여 웹 API의 단위 테스트 OData

편집 :

나는 영감이 사용하고 난 유효한 시험이 어설해야합니까? 내 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)); 
     } 

CONTROLLER 방법은

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

+0

@Rikard ... 결코 한 번도 포기하지 않았습니다. 해결책이 있으면 대답을 게시하십시오. –

+0

QueryOptions.ApplyTo를 사용하면 쿼리를 테스트 할 수 있습니다. 그 기능을 사용하는 대신 괜찮을거야? – Rikard

답변

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 */); 

알림 mocks가 인터페이스에서 작동하므로 알림을 만들어야합니다. 조롱 된 객체를 사용하여 유닛 테스트에 필요한 모든 것을 반환하도록 지시합니다.

+0

IDataQueryOptions이란 무엇입니까? IODataQueryOptions을 의미하는 경우 존재하지 않습니다. 내가 아는 ODataQueryOptions에 대한 인터페이스가 없습니다. –

+0

@BigDaddy 나는'ODataQueryOptions'의 사용법을 추상화하기 위해'IDataQueryOptions' (및 래퍼 클래스)를 생성해야한다고 말하고 있다고 생각합니다. –

+1

@PatrickQuirk ... 당신 말이 맞을지 모르지만, 이것을 테스트 할 수있는 더 좋은 방법이 있어야합니다. BTW, ODataQueryOptions은 병목 현상이없는 것 같습니다. 그것은 그것이 무엇을 돌려주고 있는지 주장하고 있습니다. 그것은 저에게 문제입니다. –

관련 문제