<?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: Unused Constructor Dependencies</title>
	<atom:link href="http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/</link>
	<description>Just another LosTechies site</description>
	<lastBuildDate>Wed, 16 Feb 2011 09:46:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: David Miller</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-13</link>
		<dc:creator>David Miller</dc:creator>
		<pubDate>Sun, 16 Aug 2009 15:27:17 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-13</guid>
		<description>The other approach which i&#039;ve seen is to use the &quot;strict&quot; behaviour of mocks. If any method is called that is not explicitly setup then an exception will be thrown.

I prefer to have a helper method:

public static TService EnsureNeverCalled&lt;TService&gt;()
{
     var mock = new Mock&lt;TService&gt;(MockBehavior.Strict);
     return mock.Object;
}

I also like to add a number of Guard clauses to the constructors of my objects - to ensure that no null dependencies are passed through. These guard clauses only verify method arguments as not null, not empty and (to a lesser degree) specific conditions. See http://ajdotnet.wordpress.com/2009/08/01/posting-guards-guard-classes-explained/</description>
		<content:encoded><![CDATA[<p>The other approach which i&#8217;ve seen is to use the &#8220;strict&#8221; behaviour of mocks. If any method is called that is not explicitly setup then an exception will be thrown.</p>
<p>I prefer to have a helper method:</p>
<p>public static TService EnsureNeverCalled<tservice>()<br />
{<br />
     var mock = new Mock</tservice><tservice>(MockBehavior.Strict);<br />
     return mock.Object;<br />
}</p>
<p>I also like to add a number of Guard clauses to the constructors of my objects &#8211; to ensure that no null dependencies are passed through. These guard clauses only verify method arguments as not null, not empty and (to a lesser degree) specific conditions. See <a href="http://ajdotnet.wordpress.com/2009/08/01/posting-guards-guard-classes-explained/" rel="nofollow">http://ajdotnet.wordpress.com/2009/08/01/posting-guards-guard-classes-explained/</a></tservice></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott_M</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-12</link>
		<dc:creator>Scott_M</dc:creator>
		<pubDate>Fri, 14 Aug 2009 15:08:28 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-12</guid>
		<description>It would be really cool if IOC containers could be smart enough to deal with the scenario of multiple dependency injection constructors.     This would eliminate the scenario of having sometimes unused dependencies.  I guess one could argue that if you find your self in the situation of having unused constructor dependencies, you should consider splitting your class into multiple classes.

</description>
		<content:encoded><![CDATA[<p>It would be really cool if IOC containers could be smart enough to deal with the scenario of multiple dependency injection constructors.     This would eliminate the scenario of having sometimes unused dependencies.  I guess one could argue that if you find your self in the situation of having unused constructor dependencies, you should consider splitting your class into multiple classes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Anderson</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-11</link>
		<dc:creator>Eric Anderson</dc:creator>
		<pubDate>Fri, 14 Aug 2009 02:54:02 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-11</guid>
		<description>I agree that the design is not optimal.  However, there are some types of classes where this is totally unavoidable.  For instance, web controller classes.  I may have different actions on a page that use different dependencies.  In that case, I&#039;d rather be intention revealing through well-named variables rather than simply passing null.

Any method that uses multiple return points is another example. Sometimes leaving those multiple return points together is better for the overall design.

I agree that the ProcessPayment() method as shown above is not optimal.  I would probably refactor out some of the responsibilities myself.  But, there are simply some classes that cannot be refactored.

As to the NullReferenceException that will come from erroneously using the null dependencies, I would rather have that than a more obscure failure.  For example, a default stub might return null or swallow a call to a void method.  This may or may not cause an error in the test.  In my experience, when these tests do fail because of the default behavior of the stub, those errors are more difficult to debug than a NullReferenceException with a stack trace that points directly to the problem.

At the end of the day, much of this is simply preference.  I&#039;m not strictly dogmatic about using named nulls when I have to, I simply like that option better than unnamed nulls or automocking containers.</description>
		<content:encoded><![CDATA[<p>I agree that the design is not optimal.  However, there are some types of classes where this is totally unavoidable.  For instance, web controller classes.  I may have different actions on a page that use different dependencies.  In that case, I&#8217;d rather be intention revealing through well-named variables rather than simply passing null.</p>
<p>Any method that uses multiple return points is another example. Sometimes leaving those multiple return points together is better for the overall design.</p>
<p>I agree that the ProcessPayment() method as shown above is not optimal.  I would probably refactor out some of the responsibilities myself.  But, there are simply some classes that cannot be refactored.</p>
<p>As to the NullReferenceException that will come from erroneously using the null dependencies, I would rather have that than a more obscure failure.  For example, a default stub might return null or swallow a call to a void method.  This may or may not cause an error in the test.  In my experience, when these tests do fail because of the default behavior of the stub, those errors are more difficult to debug than a NullReferenceException with a stack trace that points directly to the problem.</p>
<p>At the end of the day, much of this is simply preference.  I&#8217;m not strictly dogmatic about using named nulls when I have to, I simply like that option better than unnamed nulls or automocking containers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charris</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-10</link>
		<dc:creator>Charris</dc:creator>
		<pubDate>Thu, 13 Aug 2009 21:13:39 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-10</guid>
		<description>@Kyle, good points, I totally agree.  I&#039;ll likewise make checks against the null ref, I don&#039;t want others thinking there are hidden behaviors for a class.</description>
		<content:encoded><![CDATA[<p>@Kyle, good points, I totally agree.  I&#8217;ll likewise make checks against the null ref, I don&#8217;t want others thinking there are hidden behaviors for a class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kyle Szklenski</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-9</link>
		<dc:creator>Kyle Szklenski</dc:creator>
		<pubDate>Thu, 13 Aug 2009 12:32:38 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-9</guid>
		<description>Ugh. I just reread your post much slower this time. The below is what I was originally going to say. Now I&#039;ll just say this: I would never verify that a dependency is NOT used by a class in a test. This is clearly a shabby design; yes it happens sometimes, but when it does, I immediately refactor and figure out what&#039;s wrong with my design.


While it is certainly possible to have unused dependencies in your ctor, it is unwise to code your tests as if you know that, or any of your code, for that matter. That implies that your class is doing too many things at once, and that it is not very cohesive. It is also an indication that you are testing against the private parts of your system, which is incredibly vulnerable and brittle.

Another point that I&#039;ve realized lately (not necessarily all that related to the post, but definitely to some of the comments) is that null should never be passed into the ctor of an object. This implies that the outside world knows when a certain dependency doesn&#039;t need to be there, and that defeats the purpose of encapsulation. </description>
		<content:encoded><![CDATA[<p>Ugh. I just reread your post much slower this time. The below is what I was originally going to say. Now I&#8217;ll just say this: I would never verify that a dependency is NOT used by a class in a test. This is clearly a shabby design; yes it happens sometimes, but when it does, I immediately refactor and figure out what&#8217;s wrong with my design.</p>
<p>While it is certainly possible to have unused dependencies in your ctor, it is unwise to code your tests as if you know that, or any of your code, for that matter. That implies that your class is doing too many things at once, and that it is not very cohesive. It is also an indication that you are testing against the private parts of your system, which is incredibly vulnerable and brittle.</p>
<p>Another point that I&#8217;ve realized lately (not necessarily all that related to the post, but definitely to some of the comments) is that null should never be passed into the ctor of an object. This implies that the outside world knows when a certain dependency doesn&#8217;t need to be there, and that defeats the purpose of encapsulation. </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nikola Malovic</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-8</link>
		<dc:creator>Nikola Malovic</dc:creator>
		<pubDate>Thu, 13 Aug 2009 07:29:08 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-8</guid>
		<description>While I dig the idea I don&#039;t think it is wise to use it in a wild because it makes your tests fragile (now they are testing more then one thing and have more then one reason to fail)

IMHO. If this null behavior is not so important for you you should remove it from test and if it is you should create a new test for that null behavior and appropriate name.

Nikola Malovic</description>
		<content:encoded><![CDATA[<p>While I dig the idea I don&#8217;t think it is wise to use it in a wild because it makes your tests fragile (now they are testing more then one thing and have more then one reason to fail)</p>
<p>IMHO. If this null behavior is not so important for you you should remove it from test and if it is you should create a new test for that null behavior and appropriate name.</p>
<p>Nikola Malovic</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-7</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Wed, 12 Aug 2009 21:54:13 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-7</guid>
		<description>I agree with you that the second test is less desirable. 

The reason I see the test as less than desirable is because if the test doesn&#039;t care about the RequestFunds() method why should we document it in the test? This could easily cause the test to become brittle. Why make a test depend on something that has nothing to do with the success or failure of the the unit in question?

For example, if a parameter was added to the RequestFunds() method then this test would not compile and we would have to fix it before continuing. 

In an isolated case it doesn&#039;t look like a big deal but on a larger scale, with a few thousand tests, you could see how the cost of maintaining the tests go up and the original intent of the test becomes less clear.

Good post!</description>
		<content:encoded><![CDATA[<p>I agree with you that the second test is less desirable. </p>
<p>The reason I see the test as less than desirable is because if the test doesn&#8217;t care about the RequestFunds() method why should we document it in the test? This could easily cause the test to become brittle. Why make a test depend on something that has nothing to do with the success or failure of the the unit in question?</p>
<p>For example, if a parameter was added to the RequestFunds() method then this test would not compile and we would have to fix it before continuing. </p>
<p>In an isolated case it doesn&#8217;t look like a big deal but on a larger scale, with a few thousand tests, you could see how the cost of maintaining the tests go up and the original intent of the test becomes less clear.</p>
<p>Good post!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dokie</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-6</link>
		<dc:creator>Dokie</dc:creator>
		<pubDate>Wed, 12 Aug 2009 21:53:53 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-6</guid>
		<description>I don&#039;t see how this does anything to help test the method since in the real   positive case the injected interfaces are not expected to be null. In this test the only way it would attempt to use the null interfaces is if the stubbed return was altered. I would personally Guard against null injected values in the c&#039;tor.   </description>
		<content:encoded><![CDATA[<p>I don&#8217;t see how this does anything to help test the method since in the real   positive case the injected interfaces are not expected to be null. In this test the only way it would attempt to use the null interfaces is if the stubbed return was altered. I would personally Guard against null injected values in the c&#8217;tor.   </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill Sorensen</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-5</link>
		<dc:creator>Bill Sorensen</dc:creator>
		<pubDate>Wed, 12 Aug 2009 21:16:55 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-5</guid>
		<description>If I&#039;m doing constructor injection with a public constructor, I tend to check the arguments vs. null and throw ArgumentNullException.</description>
		<content:encoded><![CDATA[<p>If I&#8217;m doing constructor injection with a public constructor, I tend to check the arguments vs. null and throw ArgumentNullException.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Smith</title>
		<link>http://lostechies.com/ericanderson/2009/08/12/unused-constructor-dependencies/#comment-4</link>
		<dc:creator>Eric Smith</dc:creator>
		<pubDate>Wed, 12 Aug 2009 20:29:47 +0000</pubDate>
		<guid isPermaLink="false">/blogs/eric/archive/2009/08/12/unused-constructor-dependencies.aspx#comment-4</guid>
		<description>That first test of yours will throw a NullReferenceException if there&#039;s an attempt to GetBankInformation or RequestFunds when either of the relevant services aren&#039;t defined.  Is this intention revealing?  Dunno, just doesn&#039;t sit well with me.</description>
		<content:encoded><![CDATA[<p>That first test of yours will throw a NullReferenceException if there&#8217;s an attempt to GetBankInformation or RequestFunds when either of the relevant services aren&#8217;t defined.  Is this intention revealing?  Dunno, just doesn&#8217;t sit well with me.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
