<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>TDD and mocks</title><link>http://blogs.msdn.com/b/cellfish/archive/2008/10/13/tdd-and-mocks.aspx</link><description>First of all I must remind you that I don't like mocks and mocks aren't stubs . But recently I read a very interesting story on code without getters and its consequences . The story is about a company where the developers where asked to develop their</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: TDD and mocks</title><link>http://blogs.msdn.com/b/cellfish/archive/2008/10/13/tdd-and-mocks.aspx#9000920</link><pubDate>Wed, 15 Oct 2008 22:28:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9000920</guid><dc:creator>Emil Gustafsson</dc:creator><description>&lt;p&gt;By stub I basically mean &amp;quot;a mock without knowledge of usage expectations&amp;quot;, mor on that here: &lt;a rel="nofollow" target="_new" href="http://blogs.msdn.com/cellfish/archive/2008/04/22/mocks-are-not-stubs.aspx"&gt;http://blogs.msdn.com/cellfish/archive/2008/04/22/mocks-are-not-stubs.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I agree that the use of mocks where we only verify number of calls and not the order is a much better use of the mock object than testing for order too.&lt;/p&gt;
&lt;p&gt;Reading the example I also see how you probably missunderstood what I meant. The code in the example is part of a method used for transfering money so the behavior being tested is actually &amp;quot;transfer money&amp;quot;. Assuming we can tell the method what transaction and what accounts to use we can mock them all if we want. Since the behavior we're testing is &amp;quot;transfer money&amp;quot; and that is the public interface the only thing we can say from that perspective is that the transaction should complete and the accounts should have their balance change correctly. If we use mocks to verify that the transaction object gets commit called once and each account object have add/remove called once we are not only verifying the behavior of the interface, we're also verifying the internals of that interface. And the whole point with an interface (IMHO) is to hide the internals since they are not important. If I wanna do something like for(int i=0; i&amp;lt;ammount;i++) { accountFrom.remove(1); accountTo.add(1); } that should be my choice of implementation and should not be something I can't change because the mocks think otherwise.&lt;/p&gt;
&lt;p&gt;That is why I think mocks should be used as little as possible when you're TDD/BDD:ing. When you write your test/spec you should concentrate on what behavior the interface should have - not how the interface is implemented internally. The exception from this rule is when you want to verify a protocol os some sort where there are rules of the protocol to make calls in a certain order.&lt;/p&gt;
&lt;p&gt;And last, I agree that if you decide to use mocks you should not assert on state but rather on the (internal) behavior the mocks define. But that is something I personally don't think you should do for reasons described above.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9000920" width="1" height="1"&gt;</description></item><item><title>re: TDD and mocks</title><link>http://blogs.msdn.com/b/cellfish/archive/2008/10/13/tdd-and-mocks.aspx#8999147</link><pubDate>Tue, 14 Oct 2008 10:44:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8999147</guid><dc:creator>Keith Braithwaite</dc:creator><description>&lt;p&gt;Maybe I don't understand what you mean by a &amp;quot;stub&amp;quot; here. I don't see any return values, so what can be asserted over? &lt;/p&gt;
&lt;p&gt;With mocks, a test for commit would say only and exactly what it needed: that two different accounts are given two different commands with the same value. Note that we do not have to say anything about the order of those commands. In this way we go in the direction of decoupling, because now we don't need to check that accounts work properly as part of our check that commit works properly (or even that accounts are simulated properly with a stub). &lt;/p&gt;
&lt;p&gt;The use of mocks lets us say: assuming that accounts work properly, commit will work properly because this is how it uses accounts. Really good mocked tests don't have any assertions, which also gets us away from (writing tests about) managing state. Which is a win.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8999147" width="1" height="1"&gt;</description></item></channel></rss>