<?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: TDD: Simplifying a test with a hand rolled stub</title>
	<atom:link href="http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 11 Feb 2012 23:17:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: miluch</title>
		<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/comment-page-1/#comment-36474</link>
		<dc:creator>miluch</dc:creator>
		<pubDate>Mon, 10 May 2010 14:39:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2086#comment-36474</guid>
		<description>The smart recording of expectation on mocks was introduced a long time ago to the most popular java mocking frameworks (easyMock, mockito, jmock).This fact along with &quot;not-implemented&quot; experience (as stated in the first comment) do not convince me to write own stubs instead of let framework generate ones.</description>
		<content:encoded><![CDATA[<p>The smart recording of expectation on mocks was introduced a long time ago to the most popular java mocking frameworks (easyMock, mockito, jmock).This fact along with &#8220;not-implemented&#8221; experience (as stated in the first comment) do not convince me to write own stubs instead of let framework generate ones.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathias</title>
		<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/comment-page-1/#comment-31064</link>
		<dc:creator>Mathias</dc:creator>
		<pubDate>Thu, 28 Jan 2010 06:53:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2086#comment-31064</guid>
		<description>Intriguing issue, got me to dig in the Rhino.Mocks documentation to see if I could find anything there. The best I could come up with was the following:
var fake = MockRepository.GenerateStub();
fake.Expect(f =&gt; f.Method(null)).IgnoreArguments().Return(&quot;First&quot;).Repeat.Once();
fake.Expect(f =&gt; f.Method(null)).IgnoreArguments().Return(&quot;Second&quot;);
var first = fake.Method(&quot;ABC&quot;);
var second = fake.Method(&quot;DEF&quot;);
Assert.AreEqual(&quot;First&quot;, first);
Assert.AreEqual(&quot;Second&quot;, second);
It seems that it&#039;s doing what you are trying to do. That being said, arguably, the hand-rolled version is clearer :)</description>
		<content:encoded><![CDATA[<p>Intriguing issue, got me to dig in the Rhino.Mocks documentation to see if I could find anything there. The best I could come up with was the following:<br />
var fake = MockRepository.GenerateStub();<br />
fake.Expect(f =&gt; f.Method(null)).IgnoreArguments().Return(&#8220;First&#8221;).Repeat.Once();<br />
fake.Expect(f =&gt; f.Method(null)).IgnoreArguments().Return(&#8220;Second&#8221;);<br />
var first = fake.Method(&#8220;ABC&#8221;);<br />
var second = fake.Method(&#8220;DEF&#8221;);<br />
Assert.AreEqual(&#8220;First&#8221;, first);<br />
Assert.AreEqual(&#8220;Second&#8221;, second);<br />
It seems that it&#8217;s doing what you are trying to do. That being said, arguably, the hand-rolled version is clearer <img src='http://www.markhneedham.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/comment-page-1/#comment-31050</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Wed, 27 Jan 2010 23:23:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2086#comment-31050</guid>
		<description>Didn&#039;t think of that, good shout - that&#039;d be much simpler.</description>
		<content:encoded><![CDATA[<p>Didn&#8217;t think of that, good shout &#8211; that&#8217;d be much simpler.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy Palmer</title>
		<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/comment-page-1/#comment-31047</link>
		<dc:creator>Andy Palmer</dc:creator>
		<pubDate>Wed, 27 Jan 2010 22:02:23 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2086#comment-31047</guid>
		<description>Why did you choose to count calls, rather than use a boolean firstCall?</description>
		<content:encoded><![CDATA[<p>Why did you choose to count calls, rather than use a boolean firstCall?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peli</title>
		<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/comment-page-1/#comment-30989</link>
		<dc:creator>Peli</dc:creator>
		<pubDate>Tue, 26 Jan 2010 16:42:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2086#comment-30989</guid>
		<description>Hi Mark,

The manual mock approach works great here because the IService interface has a single method. If that interface would have many methods, you would end up with a lot of &#039;not implemented&#039; methods. This is typically why one would use a mock framework.

Now the shameless plug: if you would use the Stubs farmework that ship with Pex, you would have almost the same experience as with manual mocks, without having to dangling &#039;not implemented&#039; methods lying around. In a nutshell, Stubs allows you to replace any method of the interface with your delegate, e.g. your example would look like this:

[TestMethod]
void SomeTest() {
  int numberOfCalls = 0;
  var service = new SIService { // implements IService
    SomeMethodString = s =&gt; { // Func
      if(numberOfCalls++ == 0)
          return &quot;aValue&quot;;
      else 
          return &quot;another value&quot;;
    }     
  };
  ... // the rest of the test
}

Stubs is really just a code generator that produces the SIService type that has a delegate field and a boiler plate implementation for each method:

class SIService : IService {
    public Func SomeMethodString;
    string IService.SomeMethod(string s) {
        var f = this.SomeMethodString;
        if (f != null) return f(s);
        else throw Exception();
    }
}</description>
		<content:encoded><![CDATA[<p>Hi Mark,</p>
<p>The manual mock approach works great here because the IService interface has a single method. If that interface would have many methods, you would end up with a lot of &#8216;not implemented&#8217; methods. This is typically why one would use a mock framework.</p>
<p>Now the shameless plug: if you would use the Stubs farmework that ship with Pex, you would have almost the same experience as with manual mocks, without having to dangling &#8216;not implemented&#8217; methods lying around. In a nutshell, Stubs allows you to replace any method of the interface with your delegate, e.g. your example would look like this:</p>
<p>[TestMethod]<br />
void SomeTest() {<br />
  int numberOfCalls = 0;<br />
  var service = new SIService { // implements IService<br />
    SomeMethodString = s =&gt; { // Func<br />
      if(numberOfCalls++ == 0)<br />
          return &#8220;aValue&#8221;;<br />
      else<br />
          return &#8220;another value&#8221;;<br />
    }<br />
  };<br />
  &#8230; // the rest of the test<br />
}</p>
<p>Stubs is really just a code generator that produces the SIService type that has a delegate field and a boiler plate implementation for each method:</p>
<p>class SIService : IService {<br />
    public Func SomeMethodString;<br />
    string IService.SomeMethod(string s) {<br />
        var f = this.SomeMethodString;<br />
        if (f != null) return f(s);<br />
        else throw Exception();<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tweets that mention TDD: Simplifying a test with a hand rolled stub at Mark Needham -- Topsy.com</title>
		<link>http://www.markhneedham.com/blog/2010/01/25/tdd-simplifying-a-test-with-a-hand-rolled-stub/comment-page-1/#comment-30968</link>
		<dc:creator>Tweets that mention TDD: Simplifying a test with a hand rolled stub at Mark Needham -- Topsy.com</dc:creator>
		<pubDate>Tue, 26 Jan 2010 08:54:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2086#comment-30968</guid>
		<description>[...] This post was mentioned on Twitter by Mark Needham and planettw, Craig Taylor. Craig Taylor said: RT @markhneedham: small example of simplifying a test with a hand rolled stub - http://bit.ly/4AAYZb [...]</description>
		<content:encoded><![CDATA[<p>[...] This post was mentioned on Twitter by Mark Needham and planettw, Craig Taylor. Craig Taylor said: RT @markhneedham: small example of simplifying a test with a hand rolled stub &#8211; <a href="http://bit.ly/4AAYZb" rel="nofollow">http://bit.ly/4AAYZb</a> [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

