[英]How do I retain parameter values from the GET ActionResult to re-use in the POST ActionResult?
[英]How do i get the model from the mocked controller ActionResult method
我有以下几点:
SiteViewModel testForm = new SiteViewModel
{
SiteID = 0,
DateCreated = DateTime.Now,
Name = "A",
};
SiteController controller = new SiteController(mockSiteRepository.Object);
//// Act
SiteViewModel result = controller.Edit(testForm); // This will not work because Edit is an ActionResult... but I want to get the model that comes out the end of the ActionResult
我该如何从ActionResult方法获取模型,以便对其进行测试?
您必须将ActionResult
转换为ViewResult
。 然后,您可以访问Model
属性。
// Act
var result = controller.Edit(testForm);
// Assert
var viewResult = result as ViewResult;
Assert.That(viewResult, Is.Not.Null);
var model = viewResult.Model as SiteViewModel;
Assert.That(model, Is.Not.Null);
// do your other assertions.
请参阅此答案 ,以有趣地讨论不同的ActionResult类型
如果您的Edit
操作仅返回ViewResult
则可以更改其方法签名以表明这一点。 但是,我不建议您这样做,因为这会使您的代码更脆弱,例如,如果您将来想更改Action的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.