我在 C# mvc 控制器中有这段代码:
[HttpPost]
public ActionResult Delete(string runId)
{
if (runId == "" || runId == null)
{
return this.Json(new { error = "Null or empty params" });
}
try
{
int userId = (int)Session["UserId"];
int run = Convert.ToInt32(runId);
CloudMgr cloud = new CloudMgr(Session);
cloud.DeleteRun(userId, run);
return this.Json(new { success = true });
}
catch (Exception ex)
{
return this.Json(new { error = ex.ToString() });
}
}
如何访问 ControllerTest 中的 Json“错误”字段以检查它是否为空?
[TestMethod]
public void DeleteWrongParam()
{
WhatIfController controller = new WhatIfController();
controller.ControllerContext =
TestUtils.CreateMockSessionControllerContext().Object as ControllerContext;
JsonResult result = controller.DeleteWhatIf(null) as JsonResult;
Assert.IsNotNull(result.Data.error);
是我想做的。有任何想法吗?谢谢。