<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Aaron Feng: Grokking Rhino Mocks</title>
    <link>http://www.aaronfeng.com/articles/2007/06/17/grokking-rhino-mocks</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>Grokking Rhino Mocks</title>
      <description>&lt;p&gt;Recently I have been doing research about integrating &lt;a href="http://ayende.com/Blog/archive/2007/05/07/Rhino-Mocks-stats.aspx"&gt;Rhino Mocks&lt;/a&gt; into the project at work.  It brought back some memories because I used Rhino Mocks at my last job.  The API has changed some since I used it last, but it took me no time to get started.  In the past, we talked about experimenting with a mocking framework at my current job, but we never pursued it. &lt;/p&gt;

&lt;p&gt;Typically we write &lt;a href="http://www.martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs"&gt;stubs&lt;/a&gt; to isolate the class we are testing.  This approach works great for testing the states of the object, but it can become cumbersome when the behavior needs to be tested.  Some of the pros and cons using stubs are as follows:&lt;/p&gt;

&lt;p&gt;Pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast and light weight.&lt;/li&gt;
&lt;li&gt;Easier to write and understand.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specialized methods are required in order to verify states.&lt;/li&gt;
&lt;li&gt;Doesn't test the behavior.&lt;/li&gt;
&lt;li&gt;More non-production code needs to be written and maintained.&lt;/li&gt;
&lt;li&gt;Time consuming for complicated interactions.&lt;/li&gt;
&lt;li&gt;Requires maintenance when the logic changes.&lt;/li&gt;
&lt;li&gt;Mundane and repetitive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currently we have over 2000 tests in the project and over 100 stubs!  It would be nice if we could eliminate all the stubs.  So far I have converted around 40 tests, and everything is going much smoother than I expected.&lt;/p&gt;

&lt;p&gt;Using Rhino Mocks promotes behavior testing which has helped me to become more aware of what the code is actually doing.  The tests will fail automatically if a method is unexpectedly called.  It is also simple to ensure if expected calls are in correct sequence when order is important.  One of my favorite features is the ability to test abstract base classes.  We usually create a testable class which extends the abstract class in order to test the base behavior.  I'm not a big fan of this approach but it does work.&lt;/p&gt;

&lt;p&gt;After I converted the tests to use Rhino Mocks instead of stubs I noticed the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Removed unnecessary or duplicated method calls.&lt;/li&gt;
&lt;li&gt;General refactoring to make the code cleaner.&lt;/li&gt;
&lt;li&gt;More expressive tests using expectations (behaviors testing) 
and less asserts (state testing).&lt;/li&gt;
&lt;li&gt;Deletion of unnecessary stubs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, this has been a really good experience in taking a closer look at the tests we first wrote.  I believe if the tests are healthy the code will also be.  Of course, there's a penalty to be paid for using a mocking framework.  It caused the tests to run a little bit slower than before.  Personally, I don't mind the slower test runs if I can actually test the behavior and reduce the amount of code required.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Interesting article on mock object: &lt;a href="http://www.martinfowler.com/articles/mocksArentStubs.html#"&gt;Mocks aren't stubs&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Sun, 17 Jun 2007 23:18:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:0e2e2ad4-beab-42d6-8fee-165a6ac8af8d</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2007/06/17/grokking-rhino-mocks</link>
      <category>programming</category>
    </item>
    <item>
      <title>"Grokking Rhino Mocks" by Aaron Feng</title>
      <description>&lt;p&gt;Yes I do.&lt;/p&gt;</description>
      <pubDate>Thu, 21 Jun 2007 22:35:55 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:37d0f774-da62-469f-934a-27f530bde9b2</guid>
      <link>http://www.aaronfeng.com/articles/2007/06/17/grokking-rhino-mocks#comment-4080</link>
    </item>
    <item>
      <title>"Grokking Rhino Mocks" by David O'Hara</title>
      <description>&lt;p&gt;Just wanted to be sure you were aware that RhinoMocks will actual stub for you as well - &lt;a href="http://tinyurl.com/2urlga"&gt;http://tinyurl.com/2urlga&lt;/a&gt; . I totally missed that at first. :)&lt;/p&gt;</description>
      <pubDate>Tue, 19 Jun 2007 09:13:51 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3c1b82e6-192f-4131-a7d7-e4814d830ca6</guid>
      <link>http://www.aaronfeng.com/articles/2007/06/17/grokking-rhino-mocks#comment-4029</link>
    </item>
  </channel>
</rss>
