MVC moq unit test the object before RedirecToAction()
- by Daoming Yang
I want to test the data inside the "item" object before it redirect to another action.
public ActionResult WebPageEdit(WebPage item, FormCollection form)
{
if (ModelState.IsValid)
{
item.Description = Utils.CrossSiteScriptingAttackCheck(item.Description);
item.Content = Utils.CrossSiteScriptingAttackCheck(item.Content);
item.Title = item.Title.Trim();
item.DateUpdated = DateTime.Now;
// Other logic stuff here
webPagesRepository.Save(item);
return RedirectToAction("WebPageList");
}
Here is my Test method:
[Test]
public void Admin_WebPageEdit_Save()
{
var controller = new AdminController();
controller.webPagesRepository = DataMock.WebPageDataInit();
controller.categoriesRepository = DataMock.WebPageCategoryDataInit();
FormCollection form = DataMock.CreateWebPageFormCollection();
RedirectToRouteResult actionResult = (RedirectToRouteResult)controller.WebPageEdit(webPagesRepository.Get(1), form);
Assert.IsNotNull(actionResult);
Assert.AreEqual("WebPageList", actionResult.RouteValues["action"]);
var item = ((ViewResult)controller.WebPageEdit(webPagesRepository.Get(1), form)).ViewData.Model as WebPage;
Assert.NotNull(item);
Assert.AreEqual(2, item.CategoryID);
}
It failed at this line:
var item = ((ViewResult)controller.WebPageEdit(webPagesRepository.Get(1), form)).ViewData.Model as WebPage;
I am thinking about is there any ways to test the "item" object before it redirect to other actions?