RhinoMocks Exploration : RhinoMocks “Caching” Values?

RhinoExplorationOccasionally on the RhinoMocks mailing list a question pops up about various pieces of the framework that people struggle with. This post is the first in hopefully an ongoing series where I address specific underpinnings of RhinoMocks to you, the reader.  I’ll do my best to explain the inner workings of the code and what is going on under the covers while trying to keep it “real”.  If you have specific things you want to see, let me know and I will do my best to put together the tutorial.

That said, let’s get started…

Occasionally a question will come in about RhinoMocks caching a value and that something must be a bug in the framework.  Consider the following very simple test where we want to return the current date/time from a property getter.  You have a component with a gettable time but no setter.  In order to fake this out we use the Stub() method return a "known" value.

   1: [TestFixture]
   2: public class TimTests
   3: {
   4:     [Test]
   5:     public void DateTimeDive()
   6:     {
   7:         var stub = MockRepository.GenerateStub<IFoo>();
   8:  
   9:         stub.Stub(x => x.Current).Return(DateTime.Now);
  10:         
  11:         Thread.Sleep(5000);
  12:  
  13:         Console.WriteLine(stub.Current);
  14:     }
  15: }
  16:  
  17: public interface IFoo
  18: {
  19:     DateTime Current { get; }
  20: }

What time do you suppose is written out the console given the Thread.Sleep cal? The common thinking is that the Stub() method stores a method to be called to return the value at the time of execution when in reality what is happening is that the stub.Stub(x => x.Current).Return(DateTime.Now) line is evaluated at runtime at the time the Stub is set.  Therefore, in the above code, the Console.WriteLine(stub.Current) will actually write the time from 5 seconds (5000 milliseconds prior) when the stub call was made. In other words, when Stub() is called, the resulting value is calculated and ready to be returned, it's not deferred for execution.

What is going on is that deep in the bowels of RhinoMocks there are things called Expectations, MockStates, and Recorders.  In the code above, when Stub() is called, what happens is a two step process (but appears as one, given the fluency, or dot-dot notation, of the call).

    1. An expectation is created with a null return value (stub.Stub(x=>x.Current))
    2. The expectation is updated with the known return value (.Return(DateTime.Now))
    (Note that if you try to write code without the second piece, you will get an InvalidOperationException when you try to use the stubbed property stating:
        failed: Method 'IFoo.get_Current();' requires a return value or an exception to throw.
        System.InvalidOperationException: Method 'IFoo.get_Current();' requires a return value or an exception to throw.
        C:\dev\OSS\rhino-tools\mocks\Rhino.Mocks\Expectations\AbstractExpectation.cs(342,0): at Rhino.Mocks.Expectations.AbstractExpectation.ReturnOrThrow(IInvocation invocation, Object[] args)
        C:\dev\OSS\rhino-tools\mocks\Rhino.Mocks\Impl\StubReplayMockState.cs(63,0): at Rhino.Mocks.Impl.StubReplayMockState.DoMethodCall(IInvocation invocation, MethodInfo method, Object[] args)
        C:\dev\OSS\rhino-tools\mocks\Rhino.Mocks\Impl\ReplayMockState.cs(118,0): at Rhino.Mocks.Impl.ReplayMockState.MethodCall(IInvocation invocation, MethodInfo method, Object[] args)
        C:\dev\OSS\rhino-tools\mocks\Rhino.Mocks\MockRepository.cs(678,0): at Rhino.Mocks.MockRepository.MethodCall(IInvocation invocation, Object proxy, MethodInfo method, Object[] args)
        C:\dev\OSS\rhino-tools\mocks\Rhino.Mocks\Impl\RhinoInterceptor.cs(96,0): at Rhino.Mocks.Impl.RhinoInterceptor.Intercept(IInvocation invocation)
        c:\OSS\Castle\Tools\Castle.DynamicProxy2\Castle.DynamicProxy\AbstractInvocation.cs(202,0): at Castle.DynamicProxy.AbstractInvocation.Proceed()
        at IFooProxybc98a6ed8f1b4aa3b9ea36d89f80e6af.get_Current()
        C:\dev\OSS\rhino-tools\mocks\Rhino.Mocks.Tests\TimTests.cs(43,0): at Rhino.Mocks.Tests.TimTests.err()
    There is an "expectation" created when the call is made and that expectation is added to a recorder (There are ordered and unordered method recorders, by default (the most common scenario) you will be using unordered recorders). These recorders record the mocked object (our stub object here), the method being called, and the "expectation". The call to .Return(DateTime.Now) updates the expectation with the desired return value.  In the RhinoMocks framework this code is very simple but it's important to note this value is executed at the point this code is run.

   1: public IMethodOptions<T> Return(T objToReturn)
   2: {
   3:     expectation.ReturnValue = objToReturn;
   4:     return this;
   5: }

You can see from the code above that setting up a return value simply updated the expectation's return value.
I hope you've followed the above code.  The RhinoMocks codebase is not simple per se and you do not need to understand it in depth to use it but I think in this case it help (as I see it often pop up on the mailing list).

So what do you do if you want something to be called in a delayed fashion?  In our example I want the call to IFoo.Current to represent the DateTime.Now when I call it (as opposed to when I set it up). To do that, simply use the "Do()" method.  Here is an example that provides the same functionality as above, only the return value of the Stub is deferred until the stub is actually called.

   1: [Test]
   2: public void Realtime_results_with_Do()
   3: {
   4:     var stub = MockRepository.GenerateStub<IFoo>();
   5:  
   6:     stub.Stub(x => x.Current).Do(new Func<DateTime>(() => DateTime.Now));
   7:  
   8:     Thread.Sleep(5000);
   9:  
  10:     Console.WriteLine(stub.Current);
  11: }

I hope this helps a bit with the confusion around RhinoMocks and perceived "caching" of values.  If you have questions about this or other areas of RhinoMocks, I would love to tear into them for you, just ask!


Posted 08-21-2009 5:26 PM by Tim Barcz
Filed under:

[Advertisement]

Comments

DotNetShoutout wrote RhinoMocks Exploration : RhinoMocks “Caching” Values? - Tim Barcz - Devlicio.us
on 08-21-2009 9:12 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

Gil Zilberfeld wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-22-2009 2:26 AM

Hi Tim,

Thanks for the post.

It never occured to me that people think that the value is a value and not a function. Did you see many cases where this happens? Why do you think it's confusing?

Thanks,

Gil

Tim Barcz wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-24-2009 1:32 PM

@Gil,

I think what is confusing is when something get's executed.  It may not be that many people have issues or questions around this area, but every now and again a question pops up.  I thought there may be others out there with similar questions who never ask them.

Tim

best bookmarking sites wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 01-17-2013 5:45 PM

xedg2B I cannot thank you enough for the article post. Great.

generic viagra discount wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 01-26-2013 10:41 PM

lJCdLL This is one awesome blog post.Thanks Again. Want more.

buy discount viagra wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 02-03-2013 1:53 PM

9Z9iJI A round of applause for your article.Really looking forward to read more. Really Cool.

clomid no prescription wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 02-24-2013 9:35 PM

Hk4l51 Great, thanks for sharing this article.Really looking forward to read more. Great.

http://clomidnoprescription.beep.com/ wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 02-27-2013 12:57 PM

GFeRv7 Thanks for the article.Really looking forward to read more. Really Great.

social bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 03-02-2013 8:43 AM

R9la6L Really enjoyed this post. Really Great.

bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 03-13-2013 11:29 PM

vsN239 Really enjoyed this blog.Really looking forward to read more.

bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 03-14-2013 12:41 PM

OvlQQ9 Im thankful for the article post.Really looking forward to read more. Fantastic.

social bookmarking service wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 03-22-2013 4:49 PM

u4uRR4 Thanks again for the article post. Much obliged.

Social bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 03-23-2013 9:36 AM

GUTLvi Awesome article post.Really looking forward to read more. Fantastic.

buy social bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 04-03-2013 11:02 AM

ZeZTxh Thanks for the blog.Thanks Again. Will read on...

grizzly bears wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 04-05-2013 4:49 PM

Im grateful for the blog.Much thanks again. Fantastic.

Social bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 04-07-2013 7:35 PM

BKO0du Looking forward to reading more. Great article post.Really looking forward to read more. Cool.

social bookmarking service wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 04-13-2013 3:26 PM

EzAEAM A big thank you for your blog article.Much thanks again. Much obliged.

slr lenses wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 05-14-2013 3:34 PM

MTfJtr Thanks so much for the article.Really thank you! Awesome.

seo service wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 05-29-2013 9:22 AM

rt1Eko I loved your article.Much thanks again. Keep writing.

social bookmarking service wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 06-07-2013 2:33 AM

ZLO0qK wow, awesome blog article.Really thank you! Want more.

best social bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 06-19-2013 9:12 AM

SMOGMC Hey, thanks for the blog article.Thanks Again. Much obliged.

cheap bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 06-20-2013 12:28 PM

9y7SW3 I loved your blog post.Really thank you! Fantastic.

great service wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 07-04-2013 9:27 AM

hfoCcJ Really enjoyed this blog.Much thanks again. Cool.

moldavian news wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 07-09-2013 6:30 PM

dYLVSb Wow, great blog post.Really looking forward to read more. Really Great.

social bookmarks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 07-29-2013 7:26 AM

nQCrOl Thank you ever so for you post.Thanks Again. Will read on...

super news wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-02-2013 10:30 AM

ZMH1kM A big thank you for your blog article.Really thank you! Will read on...

greatest news wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-03-2013 11:04 AM

wzz8VF Awesome article post.Thanks Again. Want more.

awesome links for you wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-19-2013 8:49 AM

LKInil Thanks-a-mundo for the post.Really looking forward to read more. Really Great.

awesome links for you wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-19-2013 8:38 PM

qo2rLD I really enjoy the article.Really thank you! Fantastic.

awesome links for you wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 08-23-2013 12:42 AM

D1STBN I appreciate you sharing this article.Really looking forward to read more. Great.

good seo guys wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 09-03-2013 9:35 PM

7n6QsZ Say, you got a nice article.Much thanks again. Really Great.

seo service wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 09-05-2013 4:25 PM

CmbGf7 I value the blog article.Really thank you! Much obliged.

online business wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 09-13-2013 12:53 PM

igtnr4 Major thanks for the blog. Will read on...

awesome link building wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 09-24-2013 6:20 AM

oEhOrj A big thank you for your article.Really thank you! Cool.

great seo work wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 09-29-2013 3:43 AM

XLEK8L Wow, great article.Thanks Again. Fantastic.

best linkbuilding wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 10-01-2013 10:24 AM

m4VzaE Enjoyed every bit of your blog.Really thank you! Really Cool.

best link build wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 10-15-2013 9:12 PM

x3P0Lv Really appreciate you sharing this blog article. Really Cool.

top seo guys wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 10-26-2013 1:35 PM

qEoPkx Thanks again for the blog article.Thanks Again. Want more.

take a look at it! wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 10-31-2013 11:38 PM

yUfJnG Very good article post. Great.

link building wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 07-17-2014 12:12 AM

YdP0wZ I really like and appreciate your blog post.Thanks Again.

cheap backlinks wrote re: RhinoMocks Exploration : RhinoMocks “Caching” Values?
on 07-18-2014 10:04 AM

FiiDiz Great, thanks for sharing this blog article. Will read on...

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)