使用 Flurl GetResponseJson<TError>() 的单元测试代码
Unit testing code using Flurl GetResponseJson<TError>()
我正在尝试对捕获 FlurlHttpException
并调用 GetResponseJson<TError>()
的控制器进行单元测试以获取 catch 块中的错误消息。我试图模拟异常,但是 Call
属性 不允许我设置 Settings
。单元测试运行时失败,因为设置中没有 JsonSerializer。如何设置此测试?
这是我目前的尝试,但没有成功:
控制器
[Route]
public async Task<IHttpActionResult> Post(SomeModel model)
{
try
{
var id = await _serviceClient.Create(model);
return Ok(new { id });
}
catch (FlurlHttpException ex)
{
if (ex.Call.HttpStatus == HttpStatusCode.BadRequest)
return BadRequest(ex.GetResponseJson<BadRequestError>().Message);
throw;
}
}
单元测试
[TestMethod]
public async Task Post_ServiceClientBadRequest_ShouldReturnBadRequestWithMessage()
{
//Arrange
string errorMessage = "A bad request";
string jsonErrorResponse = JsonConvert.SerializeObject(new BadRequestError { Message = errorMessage });
var badRequestCall = new HttpCall
{
Response = new HttpResponseMessage(HttpStatusCode.BadRequest),
ErrorResponseBody = jsonErrorResponse
//This would work, but Settings has a private set, so I can't
//,Settings = new FlurlHttpSettings { JsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings()) }
};
_mockServiceClient
.Setup(client => client.create(It.IsAny<SomeModel>()))
.ThrowsAsync(new FlurlHttpException(badRequestCall, "exception", new Exception()));
//Act
var result = await _controller.Post(new SomeModel());
var response = result as BadRequestErrorMessageResult;
//Assert
Assert.IsNotNull(response);
Assert.AreEqual(errorMessage, response.Message);
}
如果你将Flurl的用法封装在你的ServiceClient对象中,那么我认为捕获FlurlException,提取Message,并返回一个更合适的异常也应该封装在那个服务中。这将使您的控制器更容易测试。
我正在尝试对捕获 FlurlHttpException
并调用 GetResponseJson<TError>()
的控制器进行单元测试以获取 catch 块中的错误消息。我试图模拟异常,但是 Call
属性 不允许我设置 Settings
。单元测试运行时失败,因为设置中没有 JsonSerializer。如何设置此测试?
这是我目前的尝试,但没有成功:
控制器
[Route]
public async Task<IHttpActionResult> Post(SomeModel model)
{
try
{
var id = await _serviceClient.Create(model);
return Ok(new { id });
}
catch (FlurlHttpException ex)
{
if (ex.Call.HttpStatus == HttpStatusCode.BadRequest)
return BadRequest(ex.GetResponseJson<BadRequestError>().Message);
throw;
}
}
单元测试
[TestMethod]
public async Task Post_ServiceClientBadRequest_ShouldReturnBadRequestWithMessage()
{
//Arrange
string errorMessage = "A bad request";
string jsonErrorResponse = JsonConvert.SerializeObject(new BadRequestError { Message = errorMessage });
var badRequestCall = new HttpCall
{
Response = new HttpResponseMessage(HttpStatusCode.BadRequest),
ErrorResponseBody = jsonErrorResponse
//This would work, but Settings has a private set, so I can't
//,Settings = new FlurlHttpSettings { JsonSerializer = new NewtonsoftJsonSerializer(new JsonSerializerSettings()) }
};
_mockServiceClient
.Setup(client => client.create(It.IsAny<SomeModel>()))
.ThrowsAsync(new FlurlHttpException(badRequestCall, "exception", new Exception()));
//Act
var result = await _controller.Post(new SomeModel());
var response = result as BadRequestErrorMessageResult;
//Assert
Assert.IsNotNull(response);
Assert.AreEqual(errorMessage, response.Message);
}
如果你将Flurl的用法封装在你的ServiceClient对象中,那么我认为捕获FlurlException,提取Message,并返回一个更合适的异常也应该封装在那个服务中。这将使您的控制器更容易测试。