MVC moq unit test the object before RedirecToAction()

Posted by Daoming Yang on Stack Overflow See other posts from Stack Overflow or by Daoming Yang
Published on 2009-10-05T20:50:12Z Indexed on 2010/04/02 17:03 UTC
Read the original article Hit count: 617

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about unit-testing