Cannot make Rhino Mocks return the correct type -


i started new job , 1 of first things i've been asked build unit tests code base (the company work committed automated testing integration tests , build takes forever complete).

so started nicely, started break dependencies here , there , started writing isolated unit tests i'm having issue rhino mocks not being able handle following situation:

//authenticationsessionmanager injected through constructor.   var authsession = authenticationsessionmanager.getsession(new guid(authentication.sessionid));  ((iexpirablesessioncontext)authsession).invalidateenabled = false; 

the type getsession method returns sessioncontext , can see gets casted iexpirablesessioncontext interface.

there expirablesessioncontext object inherits sessioncontext , implements iexpirablesessioncontext interface.

the way session object stored , retrieved shown in following snippet:

private readonly dictionary<guid, sessioncontext<tcontent>> sessions= new dictionary<guid, sessioncontext<tcontent>>();   public override sessioncontext<tcontent> getsession(guid sessionid) {     var session = base.getsession(sessionid);      if (session != null)     {         ((iexpirablesessioncontext)session).resettimeout();     }      return session; }  public override sessioncontext<tcontent> createsession(tcontent content) {     var session = new expirablesessioncontext<tcontent>(content, sessiontimeoutmilliseconds, new timercallback(invalidatesession));      sessions.add(session.id, session);                  return session; } 

now problem when mock call getsession, though i'm telling rhino mocks return expirablesessioncontext<...> object, test throws exception on line it's being casted iexpirablesession interface, here code in test (i know i'm using old syntax, please bear me on one):

mocks = new mockrepository(); iauthenticationsessionmanager authenticationsessionmock; authenticationsessionmock = mocks.dynamicmock<iauthenticationsessionmanager>();  var stationagentmanager = new stationagentmanager(authenticationsessionmock);  var authenticationsession = new expirablesessioncontext<authenticationsessioncontent>(new authenticationsessioncontent(anyusername, anypassword), 1, null);  using (mocks.record())   {    expect.call(authenticationsessionmock.getsession(guid.newguid())).ignorearguments().return(authenticationsession);   }  using (mocks.playback())   {       var result = stationagentmanager.startdeploymentsession(anyauthenticationcookie);     assert.isfalse(((iexpirablesessioncontext)authenticationsession).invalidateenabled);   } 

i think makes sense cast fails since method returns different kind of object , production code works since session being created correct type , stored in dictionary code test never run since being mocked.

how can set test run correctly?

thank can provide.

turns out working fine, problem on setup each test there expectation on method call:

expect.call(authenticationsessionmock.getsession(anysession.id)).return(anysession).repeat.any(); 

so expectation overriding 1 set on own test. had take expectation out of setup method, include on helper method , have other tests use 1 instead.

once out of way, test started working.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -