<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>Floehopper: The Difference between Mocks and Stubs</title>
    <link>http://blog.floehopper.org/articles/2006/09/11/the-difference-between-mocks-and-stubs</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts on the bergy bits of life</description>
    <item>
      <title>The Difference between Mocks and Stubs</title>
      <description>&lt;p&gt;Some recent converstations have made me realise that a lot of people are confused by the difference between mocking and stubbing. I&amp;#8217;ve realised that I&amp;#8217;ve only added to the confusion by calling part of the &lt;a href="http://mocha.rubyforge.com"&gt;Mocha&lt;/a&gt; library, Stubba.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;The difference between mocking and stubbing&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Stubbing a method is all about replacing the method with code that returns a specified result (or perhaps raises a specified exception). Mocking a method is all about asserting that a method has been called (perhaps with particular parameters).&lt;/p&gt;


	&lt;p&gt;If you think about it, it&amp;#8217;s difficult (or impossible?) to do mocking without stubbing &amp;#8211; you need to return from the mocked method, so that the code under test can complete execution. So mocking libraries tend to implicitly or explicitly allow you to do stubbing.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Mocking and stubbing with Mocha&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;To stub a method, use the &lt;a href="http://mocha.rubyforge.org/classes/Mocha/MockMethods.html#M000006"&gt;stubs&lt;/a&gt; method.
To mock a method, use the &lt;a href="http://mocha.rubyforge.org/classes/Mocha/MockMethods.html#M000005"&gt;expects&lt;/a&gt; method.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;The difference between Mocha and Stubba&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://mocha.rubyforge.com"&gt;Mocha&lt;/a&gt; is a traditional mocking library very much in the &lt;a href="http://www.jmock.org"&gt;JMock&lt;/a&gt; mould. Stubba is a separate part of Mocha that allows mocking &lt;strong&gt;and&lt;/strong&gt; stubbing of methods on real (non-mock) classes. It works by moving the method of interest to one side, adding a new stubbed version of the method which delegates to a traditional mock object. You can use this mock object to set up stubbed return values or set up expectations of methods to be called. After the test completes the stubbed version of the method is removed and replaced by the original.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m thinking of ditching the name Stubba, because this part of the library is not solely concerned with stubbing. Let me know what you think.&lt;/p&gt;


	&lt;p&gt;Martin Fowler has a must-read article on why &lt;a href="http://www.martinfowler.com/articles/mocksArentStubs.html"&gt;Mocks Aren&amp;#8217;t Stubs&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Another great reference is JMocks documentation &amp;#8211; in particular &lt;a href="http://www.jmock.org/yoga.html"&gt;Yoga for your Unit Tests&lt;/a&gt;.&lt;/p&gt;&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;ClassUnderTest&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;dependency&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="attribute"&gt;@dependency&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;dependency&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;do_it&lt;/span&gt;
    &lt;span class="attribute"&gt;@dependency&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;execute&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Dependency&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;execute&lt;/span&gt;
    &lt;span class="comment"&gt;# complicated code&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;strong&gt;Stubbing example&lt;/strong&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;MyTest&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Test&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Unit&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;TestCase&lt;/span&gt;
  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;DependencyStub&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;execute&lt;/span&gt;
      &lt;span class="constant"&gt;true&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;test_stubbing_example&lt;/span&gt;
    &lt;span class="ident"&gt;dependency&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;DependencyStub&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
    &lt;span class="ident"&gt;class_under_test&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;ClassUnderTest&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;dependency&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;assert_equal&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;class_under_test&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;do_it&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;strong&gt;Mocking example&lt;/strong&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;MyTest&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Test&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Unit&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;TestCase&lt;/span&gt;
  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;DependencyMock&lt;/span&gt;
    &lt;span class="ident"&gt;attr_reader&lt;/span&gt; &lt;span class="symbol"&gt;:call_count&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;
      &lt;span class="attribute"&gt;@call_count&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="number"&gt;0&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;execute&lt;/span&gt;
      &lt;span class="attribute"&gt;@call_count&lt;/span&gt; &lt;span class="punct"&gt;+=&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;test_mocking_example&lt;/span&gt;
    &lt;span class="ident"&gt;dependency&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;DependencyMock&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
    &lt;span class="ident"&gt;class_under_test&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;ClassUnderTest&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;dependency&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="ident"&gt;class_under_test&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;do_it&lt;/span&gt;
    &lt;span class="ident"&gt;assert_equal&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;dependency&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call_count&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Mon, 11 Sep 2006 02:59:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ac0cff8e-8238-4234-87e7-16fd6739d5b6</guid>
      <author>James Mead</author>
      <link>http://blog.floehopper.org/articles/2006/09/11/the-difference-between-mocks-and-stubs</link>
      <category>ruby</category>
      <category>difference</category>
      <category>mock</category>
      <category>stub</category>
      <category>expectation</category>
      <category>mocha</category>
      <category>stubba</category>
      <category>testing</category>
      <category>mocking</category>
      <category>stubbing</category>
    </item>
    <item>
      <title>"The Difference between Mocks and Stubs" by James Mead</title>
      <description>&lt;p&gt;It would be possible to make Mocha a standalone library, but not Stubba since it depends heavily on Mocha.&lt;/p&gt;


	&lt;p&gt;Also it seems a bit odd to me that you would use Stubba without using Mocha, since to my mind one of the advantages of Mocha/Stubba is the common expectation syntax.&lt;/p&gt;</description>
      <pubDate>Thu, 21 Sep 2006 04:31:29 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:64bbb5cc-1b60-4236-a9a9-bf8f78301f4d</guid>
      <link>http://blog.floehopper.org/articles/2006/09/11/the-difference-between-mocks-and-stubs#comment-93</link>
    </item>
    <item>
      <title>"The Difference between Mocks and Stubs" by Luke Redpath</title>
      <description>&lt;p&gt;I&amp;#8217;d quite like to see Stubba as a standalone library. I don&amp;#8217;t really have a need for Mocha (I like using RSpec&amp;#8217;s mocking API or in the absence of that, FlexMock) but I find Stubba invaluable.&lt;/p&gt;</description>
      <pubDate>Tue, 12 Sep 2006 20:45:01 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:43f384ac-60d0-4617-8ab1-5b552df44ec6</guid>
      <link>http://blog.floehopper.org/articles/2006/09/11/the-difference-between-mocks-and-stubs#comment-86</link>
    </item>
    <item>
      <title>"The Difference between Mocks and Stubs" by Labrat</title>
      <description>&lt;p&gt;It seems like people with a decent amount of Java experience or experienced programmers don&amp;#8217;t have trouble picking up on the difference.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m a bit curious to know the origins of mocha and the design concept behind separating it into three libraries.&lt;/p&gt;


	&lt;p&gt;I personally like the name Stubba but I guess a lot of people don&amp;#8217;t go beyond the name to see the functionality inside.  Will the renaming break previous uses of this library?&lt;/p&gt;


	&lt;p&gt;One option may be to separate out Stubba as an independent library and make it a dependency in Mocha proper (much like Rails) since it seems popular in its own right.  That way other testing frameworks or even Rails itself can integrate it if feasible.&lt;/p&gt;


	&lt;p&gt;Either way, I think it&amp;#8217;s just a matter of the rest of us catching up.  The documentation push of late is immensely helpful.&lt;/p&gt;


	&lt;p&gt;Just my 2 cents.&lt;/p&gt;</description>
      <pubDate>Mon, 11 Sep 2006 23:52:43 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:4b8c20fa-9013-434f-9e9f-173cd4386baf</guid>
      <link>http://blog.floehopper.org/articles/2006/09/11/the-difference-between-mocks-and-stubs#comment-85</link>
    </item>
  </channel>
</rss>
