【发布时间】:2019-10-08 10:15:28
【问题描述】:
我正在为我的 WebAPI Web 服务编写一些测试,但无法弄清楚如何在测试中将 JSON 发送到我的服务方法。
ScheduleRequest sr = new ScheduleRequest();
sr.Months = null;
sr.States = null;
sr.Zip = null;
sr.Miles = null;
sr.PCodes = null;
sr.PageStart = 1;
sr.PageLimit = 10;
HttpRequestMessage m = new HttpRequestMessage();
string sr_ = JsonConvert.SerializeObject(sr);
// How do I load it into the HttpRequestMessage???
// m.Content. = sr_;
var controller = new ShoppingCartController();
// Call the controlelr method and test if the return data is correct.
EventSyncResponse res = (EventSyncResponse)controller.CourseSchedule(m);
我这样做对吗?
控制器代码:
public object CourseSchedule(ScheduleRequest request)
{
try
{
var result = cart.GetCourseSchedule(request);
return Ok(result);
}
catch (Exception ex)
{
if (ex.Message.StartsWith(@"ORA-20001"))
{
return Ok(new ParticipantResponse { FirstName = "No record found" });
}
throw ex;
}
}
【问题讨论】:
-
大部分其他方法都可以通过GET进行测试,不需要JSON数据作为参数。
标签: c# json unit-testing asp.net-web-api2