2
NancyFx에서 JSON 모델 바인딩을 테스트하려고합니다.Nancy에서 단위 테스트 JSON ModelBinding
요청은 브라우저에서 테스트 할 때 작동하지만 테스트를 통과 할 수 없습니다. 테스트를 디버깅 할 때 반환 된 모델이 null 인 것을 확인했습니다.
var model = this.Bind<EventRequestModel>();
항상 null입니다. 여기
내가 뭘하는지의 간단한 예입니다NancyModule :
Post["/Events"] = _ =>
{
// Convert request to model and validate
try
{
var model = this.Bind<EventRequestModel>();
var result = this.Validate(model);
if (!result.IsValid)
throw new Exception("Model was not valid");
return HttpStatusCode.OK
}
catch (Exception ex)
{
_logger.LogError(ex);
return HttpStatusCode.BadRequest;
}
};
단위 테스트 :
[Fact]
public void ReturnOkOnGoodRequest()
{
// Create a valid model
var model = new EventRequestModel()
{
TopRightLat = 100,
TopRightLong = 100,
BottomLeftLat = 100,
BottomLeftLong = 100
};
var response = _browser.Post("/API/Events", with =>
{
with.JsonBody(model);
});
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
내가 직접 몸으로 JSON을 작성 시도 및 이렇게하면 :
var json = "{'TopRightLat' : 0, 'TopRightLong': 0, 'BottomLeftLat': 0, 'BottomLeftLong': 0}"
var response = _browser.Post("/API/Events", with =>
{
with.Header("Content-Type", "application/json");
with.Body(json);
});
이 JSON 본문은 끝점을 수동으로 테스트하지만 내 단위 테스트에서는 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?