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));
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);
Unit tests should test business logic. You don't need to write a unit test just for checking some property for null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With