<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Rhino Mocks</title>
        <link>http://ayende.com/Blog/category/487.aspx</link>
        <description>Rhino Mocks</description>
        <language>en-US</language>
        <copyright>Ayende Rahien</copyright>
        <managingEditor>Ayende@ayende.com</managingEditor>
        <generator>Subtext Version 2.0.0.43</generator>
        <item>
            <title>Broad Support, indeed</title>
            <link>http://ayende.com/Blog/archive/2008/11/14/broad-support-indeed.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/BroadSupportindeed_A6E/image_2.png"&gt;&lt;img height="42" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/BroadSupportindeed_A6E/image_thumb.png" width="575" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10610.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/11/14/broad-support-indeed.aspx</guid>
            <pubDate>Thu, 13 Nov 2008 22:44:34 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10610.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/11/14/broad-support-indeed.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10610.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Recursive Mocking</title>
            <link>http://ayende.com/Blog/archive/2008/10/10/recursive-mocking.aspx</link>
            <description>&lt;p&gt;This now works :-)&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/RecursiveMocking_BCFE/image_2.png"&gt;&lt;img height="345" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/RecursiveMocking_BCFE/image_thumb.png" width="706" border="0" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;The challenge is still open, I intentionally stopped before completing the feature, and there is a failing test in the &lt;a href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/rhino-mocks/Rhino.Mocks.Tests/RecursiveMocks.cs"&gt;RecusriveMocks&lt;/a&gt; fixture that you can start from.&lt;/p&gt; &lt;p&gt;And just to give you an idea about what I am talking about, please run this and examine the results:&lt;/p&gt; &lt;p&gt;svn diff &lt;a title="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk" href="https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk"&gt;https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk&lt;/a&gt; -r 1682:1683&lt;/p&gt; &lt;p&gt;A really cool web view of them is &lt;a href="http://demo.codesaga.com/history/RhinoTools?cs=1683"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10555.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/10/recursive-mocking.aspx</guid>
            <pubDate>Fri, 10 Oct 2008 11:40:45 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10555.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/10/10/recursive-mocking.aspx#feedback</comments>
            <slash:comments>17</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10555.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Request for comments: Changing the way dynamic mocks behave in Rhino Mocks</title>
            <link>http://ayende.com/Blog/archive/2008/10/10/request-for-comments-changing-the-way-dynamic-mocks-behave-in.aspx</link>
            <description>&lt;p /&gt; &lt;p&gt;I have just committed a change to the way Rhino Mocks handles expectations for dynamic mocks and stubs.  Previously, the meaning of this statement was "expect Foo() to be called once and return 1 when it does":&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;Expect.Call( bar.Foo ).Return(1);&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, the meaning of this is: "expect Foo() to be called one or more times, and return 1 when it does". This means that this will work:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;Assert.AreEqual(1, bar.Foo);
Assert.AreEqual(1, bar.Foo);
Assert.AreEqual(1, bar.Foo);
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Where as previously, using dynamic mocks, it would fail on the second assert, because the expectation that was setup was consumed. I think that this is a more natural way to behave, but this is a subtle breaking change.&lt;br /&gt;You can get the old behavior by specifying .Repeat.Once().&lt;/p&gt;
&lt;p&gt;Thoughts?&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10554.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/10/request-for-comments-changing-the-way-dynamic-mocks-behave-in.aspx</guid>
            <pubDate>Fri, 10 Oct 2008 10:42:00 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10554.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/10/10/request-for-comments-changing-the-way-dynamic-mocks-behave-in.aspx#feedback</comments>
            <slash:comments>23</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10554.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Rhino Mocks 3.5 Gems - Explicit Property Setting Expectations</title>
            <link>http://ayende.com/Blog/archive/2008/10/09/rhino-mocks-3.5-gems-explicit-property-setting-expectations.aspx</link>
            <description>&lt;p /&gt; &lt;blockquote&gt; &lt;p&gt;This post is derived from the Rhino Mocks 3.5 &lt;a href="http://ayende.com/Wiki/Rhino%20Mocks%203.5.ashx#PropertySettersExplicitExpectationAPI"&gt;documentation&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;Setting expectations for property set was always very simple, and slightly confusing with Rhino Mocks. Here is how you do it:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;view.Username = "&lt;span style="color: #8b0000"&gt;the user name&lt;/span&gt;";	&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;The problem is that it is &lt;em&gt;hard&lt;/em&gt; to see that there is an expectation created here. So, with the generous help of Sebastian Jancke, we have a new syntax:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;Expect.Call(view.Username).SetPropertyWithArguments("&lt;span style="color: #8b0000"&gt;the user name&lt;/span&gt;");&lt;/pre&gt;&lt;/blockquote&gt;
&lt;blockquote /&gt;
&lt;p&gt;This is much more explicit and easier to understand. We can also set expectation on the property set, without expecting a certain value using this syntax:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt; Expect.Call(view.Username).SetPropertyAndIgnoreArguments();&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://ayende.com/Blog/aggbug/10550.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/09/rhino-mocks-3.5-gems-explicit-property-setting-expectations.aspx</guid>
            <pubDate>Thu, 09 Oct 2008 07:48:52 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10550.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/10/09/rhino-mocks-3.5-gems-explicit-property-setting-expectations.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10550.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Rhino Mocks Challenge: Implement This Feature</title>
            <link>http://ayende.com/Blog/archive/2008/10/08/rhino-mocks-challenge-implement-this-feature.aspx</link>
            <description>&lt;p&gt;Okay, let us see if this approach works...&lt;/p&gt;
&lt;p&gt;Here is a description of a feature that I would like to have in Rhino Mocks (modeled after a new feature in Type Mock). I don't consider this a complicated feature, and I would like to get more involvement from the community in building Rhino Mocks (see the list of all the people that helped get Rhino Mocks 3.5 out the door).&lt;/p&gt;
&lt;p&gt;The feature is fluent mocks. The idea is that this code should work:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;var mockService = MockRespository.GenerateMock&amp;lt;IMyService&amp;gt;();&lt;br /&gt;
  Expect.Call( mockService.Identity.Name ).Return("foo");&lt;br /&gt;&lt;/p&gt;

  &lt;p&gt;Assert.AreEqual("foo", mockService.Identity.Name);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Where identity is an interface.&lt;/p&gt;
&lt;p&gt;The best place to capture such semantics is in the RecordMockState.&lt;/p&gt;
&lt;p&gt;Have fun, and send me the patch :-)&lt;/p&gt;
&lt;img src="http://ayende.com/Blog/aggbug/10547.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/08/rhino-mocks-challenge-implement-this-feature.aspx</guid>
            <pubDate>Tue, 07 Oct 2008 22:41:21 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10547.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/10/08/rhino-mocks-challenge-implement-this-feature.aspx#feedback</comments>
            <slash:comments>16</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10547.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Rhino Mocks 3.5 RTM</title>
            <link>http://ayende.com/Blog/archive/2008/10/05/rhino-mocks-3.5-rtm.aspx</link>
            <description>&lt;p&gt;Today I decided that I had enough time to get bugs for the 3.5 RC, so I fixed all the remaining bugs, updated the &lt;a href="http://ayende.com/Wiki/Rhino+Mocks+3.5.ashx"&gt;Rhino Mocks 3.5 Documentation&lt;/a&gt;, and put the &lt;a href="http://ayende.com/projects/rhino-mocks/downloads.aspx"&gt;binaries out the site&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;For this release, I actually have 4 binary packages. One for .NET 3.5 and one for .NET 2.0, but I have an additional criteria, with the castle assemblies merged (default) and with the castle assemblies included). The reason for having those two options is that people who want to &lt;a href="http://using.stormwindproject.org:8081/pages/viewpage.action?pageId=2195469"&gt;extend Rhino Mocks&lt;/a&gt; directly can do it more easily. In general, I suggest using the merged version.&lt;/p&gt;
&lt;p&gt;So, what do we actually have here (feature differences from 3.4)?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;
    &lt;a href="http://www.ayende.com/Blog/archive/2008/05/16/Rhino-Mocks--Arrange-Act-Assert-Syntax.aspx"&gt;Assert Act Arrange&lt;/a&gt; &lt;a href="http://www.ayende.com/Blog/archive/2008/06/13/The-test-of-fire-Rhino-Mocks-3.5-in-the-real.aspx"&gt;syntax&lt;/a&gt; for mocking

    &lt;ul&gt;
      &lt;li&gt;Including support for .NET 2.0&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Added a way to access the mocked method at runtime, using WhenCalled (similar to Do(), but without the pain of having to specify a special delegate).&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;CreateMock() is &lt;em&gt;deprecated&lt;/em&gt; and marked with the [Obsolete] attribute. Use StrictMock() instead.&lt;/strong&gt;&lt;/li&gt;

  &lt;li&gt;Support for mocking interface in C++ that mix native and managed types. (Note, may require that you install &lt;a href="http://ayende.com/Blog/archive/2008/09/25/kb957541-is-my-favorite-hotfix.aspx"&gt;kb957541&lt;/a&gt; to get around bug introduced to the framework on SP1).&lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;New event raising syntax:&lt;br /&gt;
  eventHolder.Raise(stub =&amp;gt; stub.Blah += null, this, EventArgs.Empty);&lt;/li&gt;

  &lt;li&gt;Better support for multi threaded replays.

    &lt;ul&gt;
      &lt;li&gt;Note that access to the mock object is now serialized.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;

  &lt;li&gt;Support AssertWasCalled on parial mocks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Patches&lt;/strong&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;From Sebastian Jancke, adding support for SetPropertyAndIgnoreArguments() and SetPropertyWithArguments( o );&lt;/li&gt;

  &lt;li&gt;From Yann Trevin, adding support for List.Element("MyKey", ...), so we are not limited to just integers.&lt;/li&gt;

  &lt;li&gt;From David Tchepak, adding support for ctor arguments when creating a mock using static method.&lt;/li&gt;

  &lt;li&gt;From Stefan Steinegger - much better support for creating inline constraints.&lt;br /&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Improvements:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Better handling of exception in raising events from mock objects&lt;/li&gt;

  &lt;li&gt;Better error message when trying to set expectation on properties of a stub.&lt;/li&gt;

  &lt;li&gt;Better error handling for AAA syntax abuse&lt;/li&gt;

  &lt;li&gt;Will give better errors if you call Verify on a mock that is in record mode.&lt;/li&gt;

  &lt;li&gt;Allowing to return to record mode without losing expectations.&lt;/li&gt;

  &lt;li&gt;BackToRecord extension method.&lt;/li&gt;

  &lt;li&gt;AAA syntax now works with Ordering&lt;/li&gt;

  &lt;li&gt;Better error message if trying to use SetupResult on stubbed mock properties.&lt;/li&gt;

  &lt;li&gt;Better error message when trying to mock null instance.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Bug fixes:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Fixing an issue with mock objects that expose methods with output parameter of type System.IntPtr.&lt;/li&gt;

  &lt;li&gt;Fixed an issue with merging, would cause issues if you are also using Castle Dynamic Proxy.&lt;/li&gt;

  &lt;li&gt;Fixed various typos&lt;/li&gt;

  &lt;li&gt;Fixed issue with mocking internal classes and interfaces.&lt;/li&gt;

  &lt;li&gt;OutRef params was not copied when creating new expectation from an existing one.&lt;/li&gt;

  &lt;li&gt;Fixing an issue with leaking expectationReplaced in mocks.&lt;/li&gt;
&lt;/ul&gt;
&lt;img src="http://ayende.com/Blog/aggbug/10541.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/10/05/rhino-mocks-3.5-rtm.aspx</guid>
            <pubDate>Sat, 04 Oct 2008 22:49:12 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10541.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/10/05/rhino-mocks-3.5-rtm.aspx#feedback</comments>
            <slash:comments>14</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10541.aspx</wfw:commentRss>
        </item>
        <item>
            <title>How .Net 3.5 SP1 broke Rhino Mocks</title>
            <link>http://ayende.com/Blog/archive/2008/08/13/How-.Net-3.5-SP1-broke-Rhino-Mocks.aspx</link>
            <description>&lt;p&gt;Okay, now that we are over the sensationalist headline, the actual problem is more complex than that. Let us assume that we have the following interface:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IComplexGeneric&amp;lt;T&amp;gt;
{
	&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; GenericMethod&amp;lt;K&amp;gt;();
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Up until .Net 3.5 SP1, Rhino Mocks was perfectly happy to deal with such an interface (well, not &lt;em&gt;happy&lt;/em&gt;, exactly, that requires some hard core code). With .Net 3.5 SP1, this generate an Execution Engine Exception.&lt;/p&gt;

&lt;p&gt;Fabian Schmied was kind enough to prove that select &lt;em&gt;is&lt;/em&gt; broken &lt;a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361606"&gt;here&lt;/a&gt;. This is the connect site issue, and I would appreciate it if you can vote for it. &lt;/p&gt;

&lt;p&gt;The work around for this would mean that I will have to remove support for mocking F#, C++ and Spec# from Rhino Mocks, and I am reluctant to do so.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10423.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/08/13/How-.Net-3.5-SP1-broke-Rhino-Mocks.aspx</guid>
            <pubDate>Wed, 13 Aug 2008 14:15:21 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/08/13/How-.Net-3.5-SP1-broke-Rhino-Mocks.aspx#feedback</comments>
            <slash:comments>22</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10423.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Mocking the file system</title>
            <link>http://ayende.com/Blog/archive/2008/07/27/Mocking-the-file-system.aspx</link>
            <description>&lt;p&gt;I have the following piece of code:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Mockingthefilesystem_47F1/image_2.png"&gt;&lt;img height="499" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Mockingthefilesystem_47F1/image_thumb.png" width="719" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And I wasn't sure about how I can test this thing. For production, this will use a File Stream, and getting those to produce errors is non trivial in most situations. A bit of thinking, however, told me that I can write a test for the error handling section of this fairly easily, like this:&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Mockingthefilesystem_47F1/image_4.png"&gt;&lt;img height="338" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Mockingthefilesystem_47F1/image_thumb_1.png" width="719" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I love having powerful tools at my reach.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10357.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/07/27/Mocking-the-file-system.aspx</guid>
            <pubDate>Sun, 27 Jul 2008 02:07:11 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/07/27/Mocking-the-file-system.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10357.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Beautiful (nontrivial) Code - Rhino Mocks 3.5's AssertWasCalled</title>
            <link>http://ayende.com/Blog/archive/2008/07/13/Beautiful-nontrivial-Code--Rhino-Mocks-3.5s-AssertWasCalled.aspx</link>
            <description>&lt;p&gt;Beautiful code is not something that is easy to define. I think of this as something that is extremely elegant, that solve a hard problem in a way that isn't brute force. I think that the way Rhino Mocks implements the AssertWasCalled functionality is elegant, and I would like to point it out.&lt;/p&gt;  &lt;p&gt;I know of at least one contributor to Rhino Mocks who consider that piece of code scary, by the way, so it is not cut &amp;amp; dry. &lt;/p&gt;  &lt;p&gt;Here is the actual method call:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AssertWasCalled&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; T mock, Action&amp;lt;T&amp;gt; action, 
	Action&amp;lt;IMethodOptions&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; setupConstraints)
{
	ExpectationVerificationInformation verificationInformation = 
		GetExpectationsToVerify(mock, action, setupConstraints);

	&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var args &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; verificationInformation.ArgumentsForAllCalls)
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (verificationInformation.Expected.IsExpected(args))
		{
			verificationInformation.Expected.AddActualCall();
		}
	}
	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (verificationInformation.Expected.ExpectationSatisfied)
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;
	&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ExpectationViolationException(
		verificationInformation.Expected.BuildVerificationFailureMessage());
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;We will get the GetExpectaionsToVerify in a bit, but broadly, it gets the expectation that should have been called and then it execute the same logic that it would have in the Record/Replay model. In fact, it is an exact reversal of the Record/Replay model. Now we record all the actual calls, and then we create an expectation and try to match it against the actual calls that were made against the actual object.&lt;/p&gt;

&lt;p&gt;Of even more interest is how we get the expectation that we are verifying:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ExpectationVerificationInformation GetExpectationsToVerify&amp;lt;T&amp;gt;(T mock, Action&amp;lt;T&amp;gt; action,
		Action&amp;lt;IMethodOptions&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;&amp;gt; setupConstraints)
{
	IMockedObject mockedObject = MockRepository.GetMockedObject(mock);
	MockRepository mocks = mockedObject.Repository;

	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (mocks.IsInReplayMode(mockedObject) == &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;)
	{
		&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InvalidOperationException(
			"&lt;span style="color: #8b0000"&gt;Cannot assert on an object that is not in replay mode.&lt;/span&gt;" +
			"&lt;span style="color: #8b0000"&gt; Did you forget to call ReplayAll() ?&lt;/span&gt;");
	}

	var mockToRecordExpectation = (T)mocks.DynamicMock(
		mockedObject.ImplementedTypes[0], 
		mockedObject.ConstructorArguments);

	action(mockToRecordExpectation);

	AssertExactlySingleExpectaton(mocks, mockToRecordExpectation);

	IMethodOptions&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt; lastMethodCall = mocks.LastMethodCall&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;&amp;gt;(mockToRecordExpectation);
	lastMethodCall.TentativeReturn();
	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (setupConstraints != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
	{
		setupConstraints(lastMethodCall);
	}
	ExpectationsList expectationsToVerify = 
		mocks.Replayer.GetAllExpectationsForProxy(mockToRecordExpectation);
	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (expectationsToVerify.Count == 0)
	{
		&lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; InvalidOperationException(
			"&lt;span style="color: #8b0000"&gt;The expectation was removed from the waiting expectations list,&lt;/span&gt;"+
			"&lt;span style="color: #8b0000"&gt; did you call Repeat.Any() ? This is not supported in AssertWasCalled()&lt;/span&gt;");
	}
	IExpectation expected = expectationsToVerify[0];
	ICollection&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[]&amp;gt; argumentsForAllCalls = mockedObject.GetCallArgumentsFor(expected.Method);
	&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ExpectationVerificationInformation
			{
				ArgumentsForAllCalls = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; List&amp;lt;&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[]&amp;gt;(argumentsForAllCalls),
				Expected = expected
			};
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is even more interesting. We create a new mocked object, and execute it in record mode against the expectation that we wish to verify. We gather this expectation and extract that from the newly created mock object, to pass it to the AssertWasCalled method, where we verify that against the actual calls made against the object.&lt;/p&gt;

&lt;p&gt;What I find elegant in the whole thing is not just the reversal of the record / replay model, it is the use of Rhino Mocks to extend Rhino Mocks.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10318.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/07/13/Beautiful-nontrivial-Code--Rhino-Mocks-3.5s-AssertWasCalled.aspx</guid>
            <pubDate>Sun, 13 Jul 2008 09:02:32 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/07/13/Beautiful-nontrivial-Code--Rhino-Mocks-3.5s-AssertWasCalled.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10318.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Where mocking fails</title>
            <link>http://ayende.com/Blog/archive/2008/07/02/Where-mocking-fails.aspx</link>
            <description>&lt;p&gt;I mentioned before that Rhino Mocks is a very powerful framework. This can be a problem at times, because it make it hard to notice that you cross the line into mock abuse.&lt;/p&gt;  &lt;p&gt;Let us look at this test for example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;[Test]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Will_raise_message_arrived_event_for_each_message_in_batch_and_across_batches()
{
	var stubbedIncomingMessageRepository = MockRepository.GenerateStub&amp;lt;IIncomingMessageRepository&amp;gt;();
	var queue = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Queue(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri("&lt;span style="color: #8b0000"&gt;queue://localhost/testQueue&lt;/span&gt;"),
						  MockRepository.GenerateStub&amp;lt;IOutgoingMessageRepository&amp;gt;(),
						  stubbedIncomingMessageRepository);
	var msgQueue = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Queue&amp;lt;QueueMessage&amp;gt;();

	stubbedIncomingMessageRepository
		.Stub(x =&amp;gt; x.GetEarliestMessage())
		.Return(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
		.Do(invocation =&amp;gt;
		{
			&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (msgQueue)
			{
				invocation.ReturnValue = msgQueue.Count == 0 ? &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; : msgQueue.Dequeue();
			}
		})
		.Repeat.Any();
	stubbedIncomingMessageRepository
		.Stub(x =&amp;gt; x.Transaction(Arg&amp;lt;Action&amp;gt;.Is.Anything))
		.Do(invocation =&amp;gt; ((Action) invocation.Arguments[0])())
		.Repeat.Any(); ;
	stubbedIncomingMessageRepository
		.Stub(x =&amp;gt; x.Save(Arg&amp;lt;QueueMessage&amp;gt;.Is.Anything))
		.Do(invocation =&amp;gt;
		{
			&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (msgQueue)
			{
				msgQueue.Enqueue((QueueMessage)invocation.Arguments[0]);
			}
		})
		.Repeat.Any();

	var callCount = 0;
	var e = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ManualResetEvent(&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);
	queue.MessageArrived += (obj =&amp;gt;
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Interlocked.Increment(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; callCount) &amp;gt;= 100)
			e.Set();
	});
	&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 50; i++)
	{
		queue.AcceptMessages(
			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; QueueMessage(), 
			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; QueueMessage()
			);
	}
	e.WaitOne();
	Assert.AreEqual(100, callCount);
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we are using Rhino Mocks to fake the entire behavior of the type. Note the thread handling in the mock object, yuck! A large portion of the test is implementing a fake message queue, in a non intuitive way.&lt;/p&gt;

&lt;p&gt;Let us explore a better way:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;[Test]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Will_raise_message_arrived_event_for_each_message_in_batch_and_across_batches()
{
	var queue = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Queue(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri("&lt;span style="color: #8b0000"&gt;queue://localhost/testQueue&lt;/span&gt;"),
						  MockRepository.GenerateStub&amp;lt;IOutgoingMessageRepository&amp;gt;(),
						  &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FakeIncomingMessageRepository());

	var callCount = 0;
	var e = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ManualResetEvent(&lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);
	queue.MessageArrived += (obj =&amp;gt;
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Interlocked.Increment(&lt;span style="color: #0000ff"&gt;ref&lt;/span&gt; callCount) &amp;gt;= 100)
			e.Set();
	});
	&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 50; i++)
	{
		queue.AcceptMessages(
			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; QueueMessage(), 
			&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; QueueMessage()
			);
	}
	e.WaitOne();
	Assert.AreEqual(100, callCount);
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; FakeIncomingMessageRepository : IIncomingMessageRepository
{
	&lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; Queue&amp;lt;QueueMessage&amp;gt; msgQueue = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Queue&amp;lt;QueueMessage&amp;gt;();

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; QueueMessage GetEarliestMessage()
	{
		&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (msgQueue)
		{
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; msgQueue.Count == 0 ? &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; : msgQueue.Dequeue();
		}
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Save(QueueMessage msg)
	{
		&lt;span style="color: #0000ff"&gt;lock&lt;/span&gt;(msgQueue)
		{
			msgQueue.Enqueue(msg);
		}
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Transaction(Action action)
	{
		action();
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we handed coded the fake object, and used that in the test. The reduction in complexity is quite significant.&lt;/p&gt;

&lt;p&gt;So, when should you avoid mocks? When there is more pain in using them than not.&lt;/p&gt;

&lt;p&gt;Not a helpful metric, but that is how I do it.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10305.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/07/02/Where-mocking-fails.aspx</guid>
            <pubDate>Wed, 02 Jul 2008 19:52:57 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/07/02/Where-mocking-fails.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10305.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>