<?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: Mock Objects: Friends Or Foes?</title>
	<atom:link href="http://gamesfromwithin.com/mock-objects-friends-or-foes/feed" rel="self" type="application/rss+xml" />
	<link>http://gamesfromwithin.com/mock-objects-friends-or-foes</link>
	<description>Living the indie life</description>
	<lastBuildDate>Thu, 17 May 2012 17:38:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Will</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-9110</link>
		<dc:creator>Will</dc:creator>
		<pubDate>Sat, 28 Aug 2010 00:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-9110</guid>
		<description>P.S. I think I hit the wrong reply button. Sorry mysterycoconut!</description>
		<content:encoded><![CDATA[<p>P.S. I think I hit the wrong reply button. Sorry mysterycoconut!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Will</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-9108</link>
		<dc:creator>Will</dc:creator>
		<pubDate>Sat, 28 Aug 2010 00:23:33 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-9108</guid>
		<description>Hi Noel! First off, reading some of your other articles got me started on unit testing and I&#039;ve been slowly getting started with that over the past few months in my hobby game project. So thanks for that and your great articles!

Anyways, I haven&#039;t really felt the need to use mocks yet since my project is small and pretty simple, and using full objects is fast enough and I don&#039;t mind repeating tests by using non-mocked dependencies. I was hoping you could give me advice by answering a few questions about your mock example:

First off it seems like counting internal function calls on objects is just repeating the code you are about to write. If you are checking getEntityInfoCallCount to be a certain number you are just saying in your implementation you should be calling the dictionary GetEntityInfo method that many times. It seems strange that one of the requirements for CreateGameEntityCallsCreateMeshForEachMesh succeeding is to only call GetEntityInfo once, since it seems like an implementation detail that doesn&#039;t really indicate whether or not CreateGameEntity is really functioning properly. For example, if you later wrote another part of the entity factory where you create a physics asset for the game entity, you might call GetEntityInfo again to check how many physics assets you need to make (of course you should probably cache it, but it doesn&#039;t seem like grounds for test failure). Is it a good idea to have your tests tightly coupled with implementation details? I&#039;ve personally been writing unit tests that check for correctness as much as possible, and implementation details as little as possible.

Second, would you recommend strictly avoiding repeating tests in favor of using mocks? So far I haven&#039;t really been avoiding that since the only downfalls I can think of are the increased test duration, increased setup time for a test, and larger number of test failures when you break an object. The test duration could probably hurt for a larger project running 10,000+ tests, or if the operation is slow (e.g. networking, file-io, etc), but it hasn&#039;t been an issue for my one person project. The setup time can be offset by using fixtures to repeat the work of creating your objects, so long as you&#039;re not using a slow operation (which would basically be the same as increasing test duration). The larger number of test failures could be slightly more time consuming to track down the root of the problem, but if you see 20 tests that reference meshes failing, you can go check your mesh factory test, and see the problem right there.

The first big benefit that I feel like I&#039;m getting by doing the repeated tests is that I know I&#039;m using the object in the same way that the game will be using it, so I&#039;m more likely to find crashes that my initial tests didn&#039;t account for. Maybe this is where I should be using functional tests? The second big benefit for me is being able to write the test exactly how I intend the object to be used. If I have too many mock objects, it seems like it clouds up unit tests as documentation of how the code should be used and what to expect from it.

Anyways, any thoughts you have would be a great help! Thanks!</description>
		<content:encoded><![CDATA[<p>Hi Noel! First off, reading some of your other articles got me started on unit testing and I&#8217;ve been slowly getting started with that over the past few months in my hobby game project. So thanks for that and your great articles!</p>
<p>Anyways, I haven&#8217;t really felt the need to use mocks yet since my project is small and pretty simple, and using full objects is fast enough and I don&#8217;t mind repeating tests by using non-mocked dependencies. I was hoping you could give me advice by answering a few questions about your mock example:</p>
<p>First off it seems like counting internal function calls on objects is just repeating the code you are about to write. If you are checking getEntityInfoCallCount to be a certain number you are just saying in your implementation you should be calling the dictionary GetEntityInfo method that many times. It seems strange that one of the requirements for CreateGameEntityCallsCreateMeshForEachMesh succeeding is to only call GetEntityInfo once, since it seems like an implementation detail that doesn&#8217;t really indicate whether or not CreateGameEntity is really functioning properly. For example, if you later wrote another part of the entity factory where you create a physics asset for the game entity, you might call GetEntityInfo again to check how many physics assets you need to make (of course you should probably cache it, but it doesn&#8217;t seem like grounds for test failure). Is it a good idea to have your tests tightly coupled with implementation details? I&#8217;ve personally been writing unit tests that check for correctness as much as possible, and implementation details as little as possible.</p>
<p>Second, would you recommend strictly avoiding repeating tests in favor of using mocks? So far I haven&#8217;t really been avoiding that since the only downfalls I can think of are the increased test duration, increased setup time for a test, and larger number of test failures when you break an object. The test duration could probably hurt for a larger project running 10,000+ tests, or if the operation is slow (e.g. networking, file-io, etc), but it hasn&#8217;t been an issue for my one person project. The setup time can be offset by using fixtures to repeat the work of creating your objects, so long as you&#8217;re not using a slow operation (which would basically be the same as increasing test duration). The larger number of test failures could be slightly more time consuming to track down the root of the problem, but if you see 20 tests that reference meshes failing, you can go check your mesh factory test, and see the problem right there.</p>
<p>The first big benefit that I feel like I&#8217;m getting by doing the repeated tests is that I know I&#8217;m using the object in the same way that the game will be using it, so I&#8217;m more likely to find crashes that my initial tests didn&#8217;t account for. Maybe this is where I should be using functional tests? The second big benefit for me is being able to write the test exactly how I intend the object to be used. If I have too many mock objects, it seems like it clouds up unit tests as documentation of how the code should be used and what to expect from it.</p>
<p>Anyways, any thoughts you have would be a great help! Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mysterycoconut</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-8023</link>
		<dc:creator>mysterycoconut</dc:creator>
		<pubDate>Tue, 27 Jul 2010 01:01:52 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-8023</guid>
		<description>@SnappyTouch are you trying to dwarf our efforts to catch up? :)</description>
		<content:encoded><![CDATA[<p>@SnappyTouch are you trying to dwarf our efforts to catch up? <img src='http://gamesfromwithin.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-8029</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Mon, 26 Jul 2010 21:57:41 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-8029</guid>
		<description>I largely agree with your analysis of mocks.  They&#039;re a useful tool to have in the unit testing arsenal, but extensively using mocks often feels a lot more wooly and fragile compared to traditional state based testing, especially when you&#039;re testing that some mock returns a canned value or something similar.  When you use the &quot;ask, don&#039;t tell&quot; design principle there&#039;s pretty much no state to verify against so interactions recorded by mocks are the only choice in many scenarios.  The other problem is that some developers will immediately start using mocking frameworks without understanding what is going on under the hood; they don&#039;t understand how stubs/mocks/spies/fakes/etc. differ.  The end result can be badly written, obfuscated and unmaintainable tests -- http://defragdev.com/blog/?p=377 .

&quot;At that point, your code design is going to start looking more like a tangled web of objects communicating in complex ways, rather than simple functions without any dependencies. You might have saved some typing time, but at what price!&quot;

It&#039;s a sliding scale, in my opinion.  The more roles and responsibilities you give to one type, the more can go wrong with it and the harder it is to test in isolation (or indeed to swap out different parts of its logic).  If a single method does 3 complicated things, it may well be advantageous to split those three things into 3 different types with distinct responsibilities.  These can be tested in isolation.  

To me, when this is done well, it doesn&#039;t feel like &#039;a tangled web of objects&#039; because the interactions are typically very simple.  You can immediately concentrate on the interface rather than the implementation.  The only caveat is that the object graph is more complex to wire up and, depending on the language &amp; tooling used, I concede this can be a big obstruction.

With C#, creating new types, extracting interfaces and navigating inheritance hierarchies is really easy thanks to tooling (ReSharper is amazing for code gen / refactoring / navigation) and the lack of header files, but even then there is a limit where numerous discrete types being to lose their allure.  As the article says, if you take the single responsibility principle to extremes, things gets silly very quickly :)

The flip side to my c# love-in is that I recently started writing some C++ again and the IDE / refactoring / code navigation support (VAX included) is not on the same level compared to what I&#039;ve grown accustomed to in the .NET world.  I&#039;m also used to using an IoC container to manage the object graph&#039;s construction and, as far as I&#039;m aware, there is no comparable construct in C++ due to the lack of reflection.  

Having to write code like var blah = new Blah(new Wargh(), new OhNo(5, 10)), new Aaaaaaaaaaaaah()) etc. is really, really sucky and brittle.  I&#039;ve seen IoC container versus manual wiring in the same codebase and the manual wiring was a terrible maintenance overhead, as altering one or two constructors / types could require changes in many places.  As a result, it seems there&#039;s pretty much no option but to back off on the object graph complexity in C++.</description>
		<content:encoded><![CDATA[<p>I largely agree with your analysis of mocks.  They&#8217;re a useful tool to have in the unit testing arsenal, but extensively using mocks often feels a lot more wooly and fragile compared to traditional state based testing, especially when you&#8217;re testing that some mock returns a canned value or something similar.  When you use the &#8220;ask, don&#8217;t tell&#8221; design principle there&#8217;s pretty much no state to verify against so interactions recorded by mocks are the only choice in many scenarios.  The other problem is that some developers will immediately start using mocking frameworks without understanding what is going on under the hood; they don&#8217;t understand how stubs/mocks/spies/fakes/etc. differ.  The end result can be badly written, obfuscated and unmaintainable tests &#8212; <a href="http://defragdev.com/blog/?p=377" rel="nofollow">http://defragdev.com/blog/?p=377</a> .</p>
<p>&#8220;At that point, your code design is going to start looking more like a tangled web of objects communicating in complex ways, rather than simple functions without any dependencies. You might have saved some typing time, but at what price!&#8221;</p>
<p>It&#8217;s a sliding scale, in my opinion.  The more roles and responsibilities you give to one type, the more can go wrong with it and the harder it is to test in isolation (or indeed to swap out different parts of its logic).  If a single method does 3 complicated things, it may well be advantageous to split those three things into 3 different types with distinct responsibilities.  These can be tested in isolation.  </p>
<p>To me, when this is done well, it doesn&#8217;t feel like &#8216;a tangled web of objects&#8217; because the interactions are typically very simple.  You can immediately concentrate on the interface rather than the implementation.  The only caveat is that the object graph is more complex to wire up and, depending on the language &amp; tooling used, I concede this can be a big obstruction.</p>
<p>With C#, creating new types, extracting interfaces and navigating inheritance hierarchies is really easy thanks to tooling (ReSharper is amazing for code gen / refactoring / navigation) and the lack of header files, but even then there is a limit where numerous discrete types being to lose their allure.  As the article says, if you take the single responsibility principle to extremes, things gets silly very quickly <img src='http://gamesfromwithin.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The flip side to my c# love-in is that I recently started writing some C++ again and the IDE / refactoring / code navigation support (VAX included) is not on the same level compared to what I&#8217;ve grown accustomed to in the .NET world.  I&#8217;m also used to using an IoC container to manage the object graph&#8217;s construction and, as far as I&#8217;m aware, there is no comparable construct in C++ due to the lack of reflection.  </p>
<p>Having to write code like var blah = new Blah(new Wargh(), new OhNo(5, 10)), new Aaaaaaaaaaaaah()) etc. is really, really sucky and brittle.  I&#8217;ve seen IoC container versus manual wiring in the same codebase and the manual wiring was a terrible maintenance overhead, as altering one or two constructors / types could require changes in many places.  As a result, it seems there&#8217;s pretty much no option but to back off on the object graph complexity in C++.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Noel</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-8028</link>
		<dc:creator>Noel</dc:creator>
		<pubDate>Mon, 26 Jul 2010 20:49:48 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-8028</guid>
		<description>Ste, You&#039;re right that the test checks for more things that a strict, by-the-book TDD approach suggest, but this article is not about TDD, just about unit tests and mocks.

That aside, I do sometimes use two or three (hardly ever more than that) check statements per TDD test when they&#039;re just clearly testing different aspects of the same thing. The name of the test plus the check statement always make it really obvious what went wrong. It comes down to personal perference I suppose.</description>
		<content:encoded><![CDATA[<p>Ste, You&#8217;re right that the test checks for more things that a strict, by-the-book TDD approach suggest, but this article is not about TDD, just about unit tests and mocks.</p>
<p>That aside, I do sometimes use two or three (hardly ever more than that) check statements per TDD test when they&#8217;re just clearly testing different aspects of the same thing. The name of the test plus the check statement always make it really obvious what went wrong. It comes down to personal perference I suppose.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ste</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-8027</link>
		<dc:creator>Ste</dc:creator>
		<pubDate>Mon, 26 Jul 2010 19:17:53 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-8027</guid>
		<description>Noel I think you have used the code you have done to simplify the hand written mock test but it would be considered by most practising TDD as wrong. A unit test should only fail for one reason and not one of many, in other words one assert per test. The test in question tests three parts where in actual fact this should be at about seven tests. Two for the amount of times GetEntityInfo is called, two for the uids and three for the amount of times CreateMesh is called (checking one, two and many calls).

I would also like to point out the a Mock is a Fake so to talk about Mocks and Fakes does not make much sense, on the other hand a Stub and a Mock are different whilst they are still both fakes.</description>
		<content:encoded><![CDATA[<p>Noel I think you have used the code you have done to simplify the hand written mock test but it would be considered by most practising TDD as wrong. A unit test should only fail for one reason and not one of many, in other words one assert per test. The test in question tests three parts where in actual fact this should be at about seven tests. Two for the amount of times GetEntityInfo is called, two for the uids and three for the amount of times CreateMesh is called (checking one, two and many calls).</p>
<p>I would also like to point out the a Mock is a Fake so to talk about Mocks and Fakes does not make much sense, on the other hand a Stub and a Mock are different whilst they are still both fakes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tweets that mention Games from Within &#124; Mock Objects: Friends Or Foes? -- Topsy.com</title>
		<link>http://gamesfromwithin.com/mock-objects-friends-or-foes/comment-page-1#comment-8022</link>
		<dc:creator>Tweets that mention Games from Within &#124; Mock Objects: Friends Or Foes? -- Topsy.com</dc:creator>
		<pubDate>Mon, 26 Jul 2010 17:04:22 +0000</pubDate>
		<guid isPermaLink="false">http://gamesfromwithin.com/?p=1059#comment-8022</guid>
		<description>[...] This post was mentioned on Twitter by Noel Llopis, Bjoern Knafla and Frederic Tessier, Bob Koon. Bob Koon said: RT @SnappyTouch: New blog post: Mock Objects: Friends Or Foes? http://bit.ly/cybNFR #tdd #gamedeveloper [...]</description>
		<content:encoded><![CDATA[<p>[...] This post was mentioned on Twitter by Noel Llopis, Bjoern Knafla and Frederic Tessier, Bob Koon. Bob Koon said: RT @SnappyTouch: New blog post: Mock Objects: Friends Or Foes? <a href="http://bit.ly/cybNFR" rel="nofollow">http://bit.ly/cybNFR</a> #tdd #gamedeveloper [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

