asp.net - How to unit test the Initialize() method of System.Web.Mvc.Controller? -


i unit test initialize method of controller object. initialize () method extracts player's id cookies collection of request object , retrieves current player object database. player object stored in controller object's currentplayer property. have following code unit test. test written index () method of 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 icontroller).execute (requestcontext);      assert.isnotnull (_chooseopponentcontroller.currentplayer);      ... // test other things index () method } 

the index () method declared as:

 [authorize, saveplayerstatus(order=2), commitchanges(order=1)]  public actionresult index ()  { ... } 

the initialize () method executed after exception following message: "object reference not set instance of object.", , stack trace looks this:

at system.web.compilation.buildmanager.getcachekeyfromvirtualpath(virtualpath virtualpath, boolean& keyfromvpp) @ system.web.compilation.buildmanager.getvpathbuildresultfromcacheinternal(virtualpath virtualpath, boolean ensureisuptodate) @ system.web.compilation.buildmanager.getvpathbuildresultinternal(virtualpath virtualpath, boolean nobuild, boolean allowcrossapp, boolean allowbuildinprecompile, boolean throwifnotfound, boolean ensureisuptodate) @ system.web.compilation.buildmanager.getvpathbuildresultwithnoassert(httpcontext context, virtualpath virtualpath, boolean nobuild, boolean allowcrossapp, boolean allowbuildinprecompile, boolean throwifnotfound, boolean ensureisuptodate) @ system.web.compilation.buildmanager.getvirtualpathobjectfactory(virtualpath virtualpath, httpcontext context, boolean allowcrossapp, boolean throwifnotfound) @ system.web.mvc.buildmanagerwrapper.system.web.mvc.ibuildmanager.fileexists(string virtualpath) @ system.web.mvc.buildmanagerviewengine.fileexists(controllercontext controllercontext, string virtualpath) @ system.web.mvc.virtualpathproviderviewengine.getpathfromgeneralname(controllercontext controllercontext, list`1 locations, string name, string controllername, string areaname, string cachekey, string[]& searchedlocations) @ system.web.mvc.virtualpathproviderviewengine.getpath(controllercontext controllercontext, string[] locations, string[] arealocations, string locationspropertyname, string name, string controllername, string cachekeyprefix, boolean usecache, string[]& searchedlocations) @ system.web.mvc.virtualpathproviderviewengine.findview(controllercontext controllercontext, string viewname, string mastername, boolean usecache) @ system.web.mvc.viewenginecollection.<>c__displayclassc.<findview>b__b(iviewengine e) @ system.web.mvc.viewenginecollection.find(func`2 lookup, boolean tracksearchedpaths) @ system.web.mvc.viewenginecollection.findview(controllercontext controllercontext, string viewname, string mastername) @ system.web.mvc.viewresult.findview(controllercontext context) @ system.web.mvc.viewresultbase.executeresult(controllercontext context) @ system.web.mvc.controlleractioninvoker.invokeactionresult(controllercontext controllercontext, actionresult actionresult) @ system.web.mvc.controlleractioninvoker.<>c__displayclass1c.<invokeactionresultwithfilters>b__19() @ system.web.mvc.controlleractioninvoker.invokeactionresultfilter(iresultfilter filter, resultexecutingcontext precontext, func`1 continuation) @ system.web.mvc.controlleractioninvoker.<>c__displayclass1c.<>c__displayclass1e.<invokeactionresultwithfilters>b__1b() @ system.web.mvc.controlleractioninvoker.invokeactionresultwithfilters(controllercontext controllercontext, ilist`1 filters, actionresult actionresult) @ system.web.mvc.controlleractioninvoker.invokeaction(controllercontext controllercontext, string actionname) 

any appreciated.

by calling execute calling entire mvc pipeline. includes view lookup logic tries go disk , find view files. part that's failing asp.net compilation system not initialized properly.

for unit test, executing much. write 2 tests: 1 verifies index method right thing, , 1 verifies initialize method right thing. else (the fact initialize gets called before action method, etc) mvc plumbing should not need worry about.


Comments

Popular posts from this blog

jQuery clickable div with working mailto link inside -

java - Getting corefrences with Standard corenlp package -

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -