I have a code block in my unit test project as below
IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;
it produce error
Error 1 'System.Web.Mvc.ActionResult' does not contain a definition for 'Model' and no extension method 'Model' accepting a first argument of type 'System.Web.Mvc.ActionResult' could be found ..
How can I solve this?
如果操作返回视图,则将其强制转换为ViewResult
((ViewResult)(IEnumerable<Product>)controller.List(2)).Model
The answer of @archil will throw an exception at run time:
Unable to cast object of type 'System.Web.Mvc.ViewResult' to type 'System.Collections.Generic.IEnumerable`1[Product]'.
The correct cast is:
IEnumerable<Product> result = (IEnumerable<Product>)((ViewResult)controller.List(2)).Model;
Or simply, change the return type of the action to a ViewResult
instead of an ActionResult
:
public ViewResult List(int param)
And in the unit test use:
IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.