[AuthorizeUser] [httpPost] [ValIDateJsonAntiForgeryToken] public ActionResult EventDetails(int eventID) { string details = this._eventDataProvIDer.GetByID(eventID).Comments; if (string.IsNullOrEmpty(details)) details = "This location has not entered any comments or further details for this event."; return Json(new { details = details }); }
控制器的测试代码:想知道我需要做些什么来测试从控制器返回的Json:
[TestMethod] public voID DetailsAreReturned() { // Arrange eventsController = new EventsController(eventDataProvIDer.Object,playerEventDataProvIDer.Object,userDataProvIDer.Object,tokenAuthent.Object,dataContext.Object,customerLocationDataProvIDer.Object); eventDataProvIDer.Setup(x => x.GetByID(1)).Returns(new Event() { Comments = "test" }); // Act JsonResult result = (JsonResult) eventsController.EventDetails(1); // Assert Assert.IsNotNull(result.Data); Assert.AreEqual(??,result); }解决方法 我必须首先赞扬这篇文章: How do I iterate over the properties of an anonymous object in C#?
var result = new JsonResult{ Data = new {details = "This location has not entered any comments or further details for this event."}};var det = result.Data.GetType().GetProperty("details",BindingFlags.Instance | BindingFlags.Public);var dataVal = det.GetValue(result.Data,null);
希望这有助于或至少为您提供一个跳跃点.
总结以上是内存溢出为你收集整理的c# – 如何从.NET MVC控制器测试JsonResult全部内容,希望文章能够帮你解决c# – 如何从.NET MVC控制器测试JsonResult所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)