我有一个返回Json Result Like的方法:
return Json(new { status = "error", message = "The organization cannot be deleted because contains transmitters!" });
现在我想测试状态和消息我尝试过这个
var result = Controller.DeleteOrganization(2) as JsonResult;
Assert.AreEqual("error", result.Data.message);
我正在错误:
对象不包含消息
的定义
如何解决此问题?
答案 0 :(得分:2)
Data
属于object
类型,不会公开该属性。尝试将Data属性分配给dynamic
变量,然后尝试访问该属性。
var result = Controller.DeleteOrganization(2) as JsonResult;
var data = JsonConvert.SerializeObject(result.Data);
var deserializedData = JsonConvert.DeserializeObject<dynamic>(data);
Assert.AreEqual("error", deserializedData.status);
答案 1 :(得分:0)
转换JSON
RootObject r = JsonConvert.DeserializeObject(json);