views:

36

answers:

0

Want to unit test the following piece of code. can some one help me out? I saw many post that we can use different open source frameworks like Rhino mocks, moq etc. But I can't figure it out what exactly required to mock how the value should intilize while creating mock objects. my class is as follows.

public class CustomWebFormViewEngine : WebFormViewEngine
{
    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        ViewEngineResult viewEngineResult = base.FindView(controllerContext, viewName + "Admin", masterName, useCache);
        return viewEngineResult;
    }
}

I have over-ridden the FindView method. Now I need to unit test the above over-ridden method. Tried different ways but in vain. viewEngineResults returning null.

Can some body put me in the right track?

The unit test method as follows

[TestMethod()] public void FindViewTest() { CustomWebFormViewEngine target = new CustomWebFormViewEngine(); string viewName = "Index"; string masterName = string.Empty; bool useCache = true; ViewEngineResult actual;

        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Home");
        routeData.Values.Add("action", "Index");

        // Create fake Controller Context
        var sessionItems = new SessionStateItemCollection();
        sessionItems["item1"] = "wow!";
        var serverVariables = new NameValueCollection();
        serverVariables["remote_addr"] = "mycomputer";

        HomeController homeController = new HomeController();

        // I am using mvcFakes project source code provided by stephenwalther
        //  I guess I am missing some required information to pass along with controllerContext
        FakeControllerContext f = new FakeControllerContext(homeController, sessionItems, serverVariables, routeData);

        actual = target.FindView(f, "IndexAdmin", masterName, false);

        // assuming that the current role of the user is Admin and Index_admin view exist in the view folder.
        Assert.AreEqual(actual.View.ToString(), "Index_Admin");
    }