[英]How do I unit test web api action method when it returns IHttpActionResult?
[英]Calling your own Action method that returns IHttpActionResult
看来我的问题不清楚。 这就是我想要做的。
我正在编写一个可以调用另一个Web API的Web API。 在我的API内部,我想从MarkAllTasksDone方法调用UpdateAssignmentStatus方法。 我不知道如何处理IHttpActionResult的返回类型,即如何处理它。 我如何获取方法中返回的信息。 在UpdateAssignmentStatus方法中,它返回带有消息的BadRequest,带有消息的InternalServerError或带有消息的Ok。 我如何获得这些数据? 抱歉,我之前应该已经提供了该信息。
只是为了在这里添加更多。 调试时,我可以看到actionResultUAS对象是我在UpdateAssignmentStatus方法中声明的返回类型之一。 BadRequest返回System.Web.Http.Results.BadRequestErrorMessageResult,Ok返回System.Web.Http.Results.OkNegotiatedContentResult,而InternalServerError返回System.Web.Http.Results.ExceptionResult。 我认为我将需要强制转换为每种类型。 是否有一个很好的方法可以做到这一点,也许是一种通用方法。
[HttpGet]
public IHttpActionResult MarkAllTasksDone(string projectID)
{
try
{
var assignments = GetAssignments("ASSGN/search?projectID=" + projectID + "&fields=taskID&fields=status" + "&apiKey=" + ApiKey);
foreach (var assignment in assignments.Where(a => a.AssignmentStatus != "DN"))
{
var actionResultUAS = UpdateAssignmentStatus(assignment.AssignmentID, "DONE");
//****how to get the ok or failure code and the returned data from the actionResultUAS object
var responseMessageUT = UpdateTaskPercentComplete(assignment.TaskID, 100);
}
}
}
catch (Exception ex)
{
return InternalServerError(ex);
}
return Ok();
}
[HttpGet]
public IHttpActionResult UpdateAssignmentStatus(string assignmentID, string assignmentStatus)
{
HttpResponseMessage httpResponse = new HttpResponseMessage();
try
{
var convertedStatus = ConvertAssignmentStatus(assignmentStatus);
httpResponse = Put("ASSGN?ID=" + assignmentID + "&status=" + convertedStatus + "&apiKey=" + ApiKey);
}
catch (ApplicationException ae)
{
return BadRequest(ae.Message);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
HttpResponseData httpResponseData = new HttpResponseData();
httpResponseData.IsSuccessful = httpResponse.IsSuccessStatusCode;
httpResponseData.Content = httpResponse.Content.ReadAsStringAsync().Result;
httpResponseData.StatusCode = httpResponse.StatusCode.ToString();
return Ok(httpResponseData);
}
您可以使用此:
return Content(httpResponse.StatusCode, httpResponse);
编辑 :当您从结果中请求对象时,您可以检查状态码并读取对象中的响应。
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
var result = httpResponse.Content.ReadAsAsync<'Your Model of assignment here'>();
}
这将返回带有响应的模型。
您需要在Api方法中返回这样的状态代码和信息,
return Content(HttpStatusCode.BadRequest, myData);
要么
return Content(HttpStatusCode.OK, myData);
然后,
try
{
var convertedStatus = ConvertAssignmentStatus(assignmentStatus);
//httpResponse = Put("ASSGN?ID=" + assignmentID + "&status=" + //convertedStatus + "&apiKey=" + ApiKey);
var http = new HttpClient { BaseAddress =
new Uri("http://localhost:11111/") };
//I'm passing the value as query string. See the bellow line
string apiUrl = "api/ControllerName/ASSGN?ID=" + assignmentID +
"&status=" + convertedStatus + "&apiKey=" + ApiKey";
var _response = http.PutAsJsonAsync(http.BaseAddress + apiUrl, "").Result;
var returnStatus = _response.StatusCode;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.