views:

77

answers:

1

Hi folks, i've got a controller method which returns a RedirectToActionResult (sucess!) or a ViewResult (failed with error messages).

If the business logic fails, i add the error messages to the AddModelError property.

Is there any way i can test this in my MS Unit tests? I also have moq, if that helps too. (i don't believe moq is required for this scenario though) .. i'm not using anything from the Request object.

thanks!

+1  A: 

Yep, figured it out.

// Arrange.
// .. whatever ..

// Act.
var viewResult = controller.Create(new Post()) as ViewResult;

// Assert.
Assert.IsNotNull(viewResult);
Assert.IsNotNull(viewResult.ViewData.ModelState["subject"]);
Assert.IsNotNull(viewResult.ViewData.ModelState["subject"].Errors);
Assert.IsTrue(viewResult.ViewData.ModelState["subject"].Errors.Count == 1);
Pure.Krome