<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Diving in to Mock Objects</title>
	<atom:link href="http://thepursuitofalife.com/diving-in-to-mock-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://thepursuitofalife.com/diving-in-to-mock-objects/</link>
	<description>Wouldn&#039;t you rather be writing code?</description>
	<lastBuildDate>Thu, 11 Mar 2010 14:41:58 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.3</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Wes Maldonado</title>
		<link>http://thepursuitofalife.com/diving-in-to-mock-objects/comment-page-1/#comment-125</link>
		<dc:creator>Wes Maldonado</dc:creator>
		<pubDate>Mon, 16 Jun 2008 15:13:23 +0000</pubDate>
		<guid isPermaLink="false">http://xidey.wordpress.com/?p=763#comment-125</guid>
		<description>I tend to use mocks most when testing the behavior of a resources controlled by a collaborating object.  Consider a model that should add itself to a search service when it is saved or updated, you could stub the search service creating the correct interface, implementation, etc. or you could verify the behavior of your model by using a mock.  I believe the behavior is the most obvious way to test this and therefore I would use a mock.  Here is an example using rspec:

describe SafetyInspectionForm, &quot;when saving&quot; do
   it &quot; should add itself to the search service when saving&quot; do
       search_service = mock(&quot;search service&quot;)
       search_service.should_receive(:add).once
       # imagine I set this up via injection or a public setter
       # e.g. form.search_service = search_service
       form.save!
       # search_service.verify implicitly called
   end
end

Compared to creating a stub to take the hits from my model, then interrogate it to determine if the behavior was correct by analyzing state I can just define my behavior and test it directly.  I don&#039;t use mocks very frequently but they are useful.  They clearly indicate the intent of the test.  Compare to a hand grown mock that we wrote in js spec.

SomeModel.describe(&#039;model marked deleted&#039;, {

  &#039;should persist internals to google gears&#039; : function(){
      model = factory.new_model();
      internal_thing = model.get_internal_thing();
      internal_thing.save = function(){ room.save_called = true; }
      model.delete();
      value_of(internal_thing.save_called).should_be(true);
  }
}

Food for thought.  I find that the first few days with mocks are frustrating because you feel like you are stuck fixing your &quot;mock&quot; while trying to discover the correct interface.  This interface discovery is the important thing though.  You can achieve similar results with lots of test driving, but then you would actually have to write the object you&#039;re interested in working with and once code it put down in the editor most are reluctant to change it.</description>
		<content:encoded><![CDATA[<p>I tend to use mocks most when testing the behavior of a resources controlled by a collaborating object.  Consider a model that should add itself to a search service when it is saved or updated, you could stub the search service creating the correct interface, implementation, etc. or you could verify the behavior of your model by using a mock.  I believe the behavior is the most obvious way to test this and therefore I would use a mock.  Here is an example using rspec:</p>
<p>describe SafetyInspectionForm, &#8220;when saving&#8221; do<br />
   it &#8221; should add itself to the search service when saving&#8221; do<br />
       search_service = mock(&#8221;search service&#8221;)<br />
       search_service.should_receive(:add).once<br />
       # imagine I set this up via injection or a public setter<br />
       # e.g. form.search_service = search_service<br />
       form.save!<br />
       # search_service.verify implicitly called<br />
   end<br />
end</p>
<p>Compared to creating a stub to take the hits from my model, then interrogate it to determine if the behavior was correct by analyzing state I can just define my behavior and test it directly.  I don&#8217;t use mocks very frequently but they are useful.  They clearly indicate the intent of the test.  Compare to a hand grown mock that we wrote in js spec.</p>
<p>SomeModel.describe(&#8217;model marked deleted&#8217;, {</p>
<p>  &#8217;should persist internals to google gears&#8217; : function(){<br />
      model = factory.new_model();<br />
      internal_thing = model.get_internal_thing();<br />
      internal_thing.save = function(){ room.save_called = true; }<br />
      model.delete();<br />
      value_of(internal_thing.save_called).should_be(true);<br />
  }<br />
}</p>
<p>Food for thought.  I find that the first few days with mocks are frustrating because you feel like you are stuck fixing your &#8220;mock&#8221; while trying to discover the correct interface.  This interface discovery is the important thing though.  You can achieve similar results with lots of test driving, but then you would actually have to write the object you&#8217;re interested in working with and once code it put down in the editor most are reluctant to change it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
