tôi có mã này trong C# khiển MVC:Làm thế nào để truy cập dữ liệu JsonResult khi kiểm tra trong ASP.NET 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() });
}
}
Làm thế nào tôi có thể truy cập Json "lỗi" lĩnh vực của tôi trong một ControllerTest để kiểm tra xem nó là null hay không?
[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);
là những gì tôi muốn làm. Bất kỳ ý tưởng? Cảm ơn.
Không gian tên cho lớp Kết quả là gì? Cảm ơn. – juanchoelx
@juanchoelx Kết quả là lớp được định nghĩa của riêng bạn như lớp này Kết quả {public bool success; lỗi chuỗi công khai} – Devesh
@juanchoelx Tôi đã cập nhật mã của mình trong câu trả lời của mình. – Devesh