Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get the Model from an ViewResult in a asp.net mvc unittest?

i call a controller action in a unit test.

 ViewResult result = c.Index(null,null) as ViewResult;

I cast the result to a ViewResult, because that is what i'm returning in the controller:

return View(model);

But how can i access this model variable in my unit test?

like image 593
Michel Avatar asked Dec 13 '22 22:12

Michel


1 Answers

// Arrange
var c = new MyController();

//Act
var result = c.Index(null,null);
var model = result.ViewData.Model; 

//Assert
Assert("model is what you want");
like image 160
Daniel Elliott Avatar answered Feb 22 '23 23:02

Daniel Elliott