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;
}
어떤 방식 으로든 작동 시키셨습니까? – Rikard
@Rikard ... 결코 한 번도 포기하지 않았습니다. 해결책이 있으면 대답을 게시하십시오. –
QueryOptions.ApplyTo를 사용하면 쿼리를 테스트 할 수 있습니다. 그 기능을 사용하는 대신 괜찮을거야? – Rikard