How to check Model properties in unit test

asp.net-mvc-3, c#, moq, razor, unit-testing

Solution

I would tend to write the asserts as follows (using Microsoft test framework asserts here - you didn't specify nunit):

// Act
ActionResult result = controller.SaveAndExit(viewModel);

// Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
ViewResult viewResult = (ViewResult)result;

Assert.IsInstanceOfType(viewResult.Model, typeof(ViewModel1));
ViewModel1 model = (ViewModel1)viewResult.Model;

Assert.IsNotNull(model.Reg);

Problem

I have a `Action` as bellow: ``` public ActionResult SaveAndExit() { ViewModel1 viewModel = new ViewModel1(); return View("Index", viewModel); } ``` In Unit Test I want to check if view `Reg` in viewModel is null or not. any suggestions please Test: ``` //act var result = controller.SaveAndExit(viewModel) as ViewResult; //assert //Assert.IsNotNull(!result.Model["Reg"].Equals(null)); ```

Original source