<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Does stubbing make your tests brittle?</title>
	<atom:link href="http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/feed/" rel="self" type="application/rss+xml" />
	<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/</link>
	<description>The blog with the bug.</description>
	<lastBuildDate>Mon, 26 Oct 2009 20:00:20 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Donnie</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-814</link>
		<dc:creator>Donnie</dc:creator>
		<pubDate>Tue, 11 Nov 2008 07:48:43 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-814</guid>
		<description>Refactoring aside stubbing/mocking (which I refer to interchangeably as &quot;stubbing&quot;) should not be coupled to the implementation. Why? Simple: coupling to implementation is a break in encapsulation which violates a core tenet of OO. Now, there is of course times when stubbing makes perfect sense, like when &quot;stubbing&quot; out some functionality that is _external to the code being tested_. I recently moved from C++ to Ruby and I&#039;m finding that oftentimes the dynamism of Ruby is taken as license by developers to throw away good OOP practices like encapsulation. It&#039;s high time for the community to recognize that the principles of OO should be maintained even if your language allows you to ignore them. I have seen tests that stub out every single method call that the method being tested calls just to verify that the other methods (oftentimes internal private methods) are being called. This pattern is followed ad nauseum until the entire unit test is tightly coupled to the entire implementation of the class. The smallest bit of refactoring grossly renders all the tests useless. Why? Because encapsulation is broken! 

Clearly this is an extreme. However, I find that more often than not OO principles are ignored in favor of a quick and dirty attempt at reaching full test coverage. This is backwards. Breaking encapsulation should be entirely avoided aside from a few exceptional cases. Instead, we have encapsulation being observed in a few exceptional cases.

So, Szeryf, I think we agree: stubbing can make your tests brittle. The reason this is so is because stubs tightly coupled to implementation break encapsulation.</description>
		<content:encoded><![CDATA[<p>Refactoring aside stubbing/mocking (which I refer to interchangeably as &#8220;stubbing&#8221;) should not be coupled to the implementation. Why? Simple: coupling to implementation is a break in encapsulation which violates a core tenet of OO. Now, there is of course times when stubbing makes perfect sense, like when &#8220;stubbing&#8221; out some functionality that is _external to the code being tested_. I recently moved from C++ to Ruby and I&#8217;m finding that oftentimes the dynamism of Ruby is taken as license by developers to throw away good OOP practices like encapsulation. It&#8217;s high time for the community to recognize that the principles of OO should be maintained even if your language allows you to ignore them. I have seen tests that stub out every single method call that the method being tested calls just to verify that the other methods (oftentimes internal private methods) are being called. This pattern is followed ad nauseum until the entire unit test is tightly coupled to the entire implementation of the class. The smallest bit of refactoring grossly renders all the tests useless. Why? Because encapsulation is broken! </p>
<p>Clearly this is an extreme. However, I find that more often than not OO principles are ignored in favor of a quick and dirty attempt at reaching full test coverage. This is backwards. Breaking encapsulation should be entirely avoided aside from a few exceptional cases. Instead, we have encapsulation being observed in a few exceptional cases.</p>
<p>So, Szeryf, I think we agree: stubbing can make your tests brittle. The reason this is so is because stubs tightly coupled to implementation break encapsulation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jogue fora os testes quebradiços : Learning on Rails</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-768</link>
		<dc:creator>Jogue fora os testes quebradiços : Learning on Rails</dc:creator>
		<pubDate>Fri, 24 Oct 2008 17:28:25 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-768</guid>
		<description>[...] Does stubbing make your tests brittle? [...]</description>
		<content:encoded><![CDATA[<p>[...] Does stubbing make your tests brittle? [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: szeryf</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-448</link>
		<dc:creator>szeryf</dc:creator>
		<pubDate>Sun, 10 Feb 2008 09:40:14 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-448</guid>
		<description>David, of course it&#039;s natural that changes to code could break the tests. But that should be big, important changes, not tweaking some internal implementation details. I think good tests should be at least partly immune to implementation changes.

But I agree, divide and conquer is a good method to minimize possible test failures. Dividing between code that changes and code that doesn&#039;t or between DB-stuff and non-DB-stuff is a good thing.</description>
		<content:encoded><![CDATA[<p>David, of course it&#8217;s natural that changes to code could break the tests. But that should be big, important changes, not tweaking some internal implementation details. I think good tests should be at least partly immune to implementation changes.</p>
<p>But I agree, divide and conquer is a good method to minimize possible test failures. Dividing between code that changes and code that doesn&#8217;t or between DB-stuff and non-DB-stuff is a good thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Lowenfels</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-445</link>
		<dc:creator>David Lowenfels</dc:creator>
		<pubDate>Sun, 10 Feb 2008 05:40:58 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-445</guid>
		<description>Firstly, I think it&#039;s unrealistic to expect that code changes will never break a test. You can&#039;t write code without understanding your system. And changing a few tests is really not a huge deal, it&#039;s simply the cost of changing your code. We are ahead of the game knowing when our code is working or not, thanks to the test suite.

However that being said, the problem you illustrate suggests to me that you could break down your methods to use smaller private methods that call the database stuff. This has the additional advantage of being self-commenting if you name them well. For instance:

def todays_cheezburger
  self.find_by_last_cheezburger_date( Date.today )
end

now your method becomes like so:

def can_has_cheezburger?
  todays_cheezburger.nil?
end

and the two methods can be tested independently of each other. Divide and conquer! :)</description>
		<content:encoded><![CDATA[<p>Firstly, I think it&#8217;s unrealistic to expect that code changes will never break a test. You can&#8217;t write code without understanding your system. And changing a few tests is really not a huge deal, it&#8217;s simply the cost of changing your code. We are ahead of the game knowing when our code is working or not, thanks to the test suite.</p>
<p>However that being said, the problem you illustrate suggests to me that you could break down your methods to use smaller private methods that call the database stuff. This has the additional advantage of being self-commenting if you name them well. For instance:</p>
<p>def todays_cheezburger<br />
  self.find_by_last_cheezburger_date( Date.today )<br />
end</p>
<p>now your method becomes like so:</p>
<p>def can_has_cheezburger?<br />
  todays_cheezburger.nil?<br />
end</p>
<p>and the two methods can be tested independently of each other. Divide and conquer! :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pat Maddox</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-441</link>
		<dc:creator>Pat Maddox</dc:creator>
		<pubDate>Mon, 04 Feb 2008 06:53:20 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-441</guid>
		<description>Hey szeryf, I &lt;a href=&quot;http://evang.eli.st/blog/2008/2/4/sorry-you-re-just-not-my-type&quot; rel=&quot;nofollow&quot;&gt;wrote a response&lt;/a&gt; to this post.  Hope it gives you some ideas.</description>
		<content:encoded><![CDATA[<p>Hey szeryf, I <a href="http://evang.eli.st/blog/2008/2/4/sorry-you-re-just-not-my-type" rel="nofollow">wrote a response</a> to this post.  Hope it gives you some ideas.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: szeryf</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-417</link>
		<dc:creator>szeryf</dc:creator>
		<pubDate>Tue, 08 Jan 2008 12:55:27 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-417</guid>
		<description>Anonymous: you have a good point, but tests in the above example weren&#039;t meant to be realistic :) It was just simplest what I could invent that showed what I was talking about. In reality I would probably try to setup fixtures, just as you say. But your questions (which are perfectly valid) also show that there are more problems waiting for somebody that uses stubbing without caution.

I think I understand difference between stubing and mocks, but in this situation the risk is IMO similar. If I used mock object instead of stubbing, it would also probably answer only to &lt;code&gt;count&lt;/code&gt; method, leading to the same problem. Is there any real difference?

Maybe we (me and my team) are doing something wrong way (that&#039;s quite likely, because it&#039;s our first project in Rails as team), but in our project most of the code in model class is somehow associated with database: finders, updaters, checkers and so on. So, most of our models tests would be integration tests by your standards. Do you have any advice on how to avoid coupling models to DB?</description>
		<content:encoded><![CDATA[<p>Anonymous: you have a good point, but tests in the above example weren&#8217;t meant to be realistic :) It was just simplest what I could invent that showed what I was talking about. In reality I would probably try to setup fixtures, just as you say. But your questions (which are perfectly valid) also show that there are more problems waiting for somebody that uses stubbing without caution.</p>
<p>I think I understand difference between stubing and mocks, but in this situation the risk is IMO similar. If I used mock object instead of stubbing, it would also probably answer only to <code>count</code> method, leading to the same problem. Is there any real difference?</p>
<p>Maybe we (me and my team) are doing something wrong way (that&#8217;s quite likely, because it&#8217;s our first project in Rails as team), but in our project most of the code in model class is somehow associated with database: finders, updaters, checkers and so on. So, most of our models tests would be integration tests by your standards. Do you have any advice on how to avoid coupling models to DB?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: szeryf</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-416</link>
		<dc:creator>szeryf</dc:creator>
		<pubDate>Tue, 08 Jan 2008 12:25:49 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-416</guid>
		<description>Mutwin: but still your controller specs are coupled to the controller class and sometimes even to the model class that the controller operates on (if I understand you correctly). But keeping controllers as simple as possibe could be a good way to reduce consequences of this coupling.</description>
		<content:encoded><![CDATA[<p>Mutwin: but still your controller specs are coupled to the controller class and sometimes even to the model class that the controller operates on (if I understand you correctly). But keeping controllers as simple as possibe could be a good way to reduce consequences of this coupling.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-415</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 08 Jan 2008 12:12:04 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-415</guid>
		<description>Any time you start stubbing methods in the class under test, you will potentially have problems. In this case, the tested class&#039; functionality is so tightly coupled to the database, it&#039;s probably a better strategy to go ahead and test it against the database. How do you currently test whether the query in &quot;count&quot; is correct or not? What if the database schema changes?

Also, mocking and stubbing are quite different and have different risks. You mention mocking has having the same effects as stubbing and that&#039;s often not true.

In general, we write integration tests of classes with tight coupling to the external resources like databases. Most of the code doesn&#039;t have this coupling so we test those with unit testing techniques. The result is a large set of very fast and stable unit tests and a smaller, slower set of integration tests.</description>
		<content:encoded><![CDATA[<p>Any time you start stubbing methods in the class under test, you will potentially have problems. In this case, the tested class&#8217; functionality is so tightly coupled to the database, it&#8217;s probably a better strategy to go ahead and test it against the database. How do you currently test whether the query in &#8220;count&#8221; is correct or not? What if the database schema changes?</p>
<p>Also, mocking and stubbing are quite different and have different risks. You mention mocking has having the same effects as stubbing and that&#8217;s often not true.</p>
<p>In general, we write integration tests of classes with tight coupling to the external resources like databases. Most of the code doesn&#8217;t have this coupling so we test those with unit testing techniques. The result is a large set of very fast and stable unit tests and a smaller, slower set of integration tests.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mutwin Kraus</title>
		<link>http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-412</link>
		<dc:creator>Mutwin Kraus</dc:creator>
		<pubDate>Mon, 07 Jan 2008 17:12:08 +0000</pubDate>
		<guid isPermaLink="false">http://szeryf.wordpress.com/2008/01/07/does-stubbing-make-your-tests-brittle/#comment-412</guid>
		<description>My solution is to use as littlte mocking/stubbing in Model specs (mostly when these models interact with others) and completely mock/stub the Controller (and view) specs. This makes changes relatively easy while encouraging to keep your controllers simple.</description>
		<content:encoded><![CDATA[<p>My solution is to use as littlte mocking/stubbing in Model specs (mostly when these models interact with others) and completely mock/stub the Controller (and view) specs. This makes changes relatively easy while encouraging to keep your controllers simple.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
