Reputation: 7564
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?
Upvotes: 2
Views: 1419
Reputation: 7336
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;
Upvotes: 0
Reputation: 39501
If action returns view, cast it to ViewResult
((ViewResult)(IEnumerable<Product>)controller.List(2)).Model
Upvotes: 2