Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3964144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:17:56+00:00 2026-05-20T03:17:56+00:00

I would like to unit test the Initialize method of a Controller object. The

  • 0

I would like to unit test the Initialize method of a Controller object. The Initialize () method basically extracts the player’s ID from the cookies collection of the request object and retrieves the current player object from the database. Then the player object is stored in the controller object’s CurrentPlayer property. I have the following code for the unit test. This test is basically written for the Index () method of the controller:

[Test]
public void Index_ReturnsJsonResult ()
{
   var _gameRepositoryMock = GameRepositoryCreator.Create (5);

   var _formsAuthenticationMock = new Mock<IFormsAuthentication> ();

   var _chooseOpponentController = new ChooseOpponentController (_gameRepositoryMock.Object, _formsAuthenticationMock.Object);


    var cookie = new HttpCookie (cookieName);
    cookie.Value = player.PlayerID + "_encrypted";

    var cookies = new HttpCookieCollection ();
    cookies.Add (cookie);

    var httpRequestMock = new Mock<HttpRequestBase> ();
    httpRequestMock.Setup (x => x.Cookies).Returns (cookies);
    httpRequestMock.Setup (x => x.IsAuthenticated).Returns (true);

    var httpContextMock = new Mock<HttpContextBase> ();
    httpContextMock.Setup (x => x.Request).Returns (httpRequestMock.Object);

    var rd = new RouteData ();
    rd.Values.Add ("action", "Index");
    rd.Values.Add ("controller", "ChooseOpponent");

    var requestContext = new RequestContext (httpContextMock.Object, rd); 

    _formsAuthenticationMock.Setup (x => x.Decrypt (cookie.Value)).Returns (player.PlayerID + "");   

    (_chooseOpponentController as IController).Execute (requestContext);

    Assert.IsNotNull (_chooseOpponentController.CurrentPlayer);

    ... // test other things for the Index () method
}

The Index () method is declared as:

 [Authorize, SavePlayerStatus(Order=2), CommitChanges(Order=1)]
 public ActionResult Index ()
 { ... }

The Initialize () method is executed successfully but after that I get an exception with the following message: “Object reference not set to an instance of an object.”, and the stack trace looks like this:

at System.Web.Compilation.BuildManager.GetCacheKeyFromVirtualPath(VirtualPath virtualPath, Boolean& keyFromVPP)
at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath)
at System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)
at System.Web.Mvc.VirtualPathProviderViewEngine.FindView(ControllerContext controllerContext, String viewName, String masterName, Boolean useCache)
at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClassc.<FindView>b__b(IViewEngine e)
at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
at System.Web.Mvc.ViewEngineCollection.FindView(ControllerContext controllerContext, String viewName, String masterName)
at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:17:56+00:00Added an answer on May 20, 2026 at 3:17 am

    By calling Execute you are essentially calling the entire MVC pipeline. This includes the view lookup logic that tries to go to disk and find your view files. The part that’s failing is the ASP.NET compilation system that is not initialized properly.

    For a unit test, you are executing too much. I would write 2 tests: one that verifies that the Index method does the right thing, and one that verifies that the Initialize method does the right thing. Everything else (the fact that Initialize gets called before the action method, etc) is MVC plumbing that you should not need to worry about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My main JavaScript framework is jQuery , so I would like my unit test
I would like to know what a Unit test and Web Test is in
I would like to be able to add a message to a unit test,
I would like to know what would be the best way to do unit
I would like to test a string containing a path to a file for
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.