<?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: Test only constructors</title>
	<atom:link href="http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Tue, 16 Mar 2010 05:27:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: miluch</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-29865</link>
		<dc:creator>miluch</dc:creator>
		<pubDate>Wed, 06 Jan 2010 09:38:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-29865</guid>
		<description>Hi

I also bumped into invariants checking in constructor/factory dilemma. 

The main drawback of test-only constructors/factories is that you can not guarantee that none of your colleagues will use them in production code !!!

On the other side, calling production-available (real) constructors/factories in your test can be very painfull...

Invariant checks inside User.CreateUserFor(headers) are quite trivial so it does not seem to be the case  but imagine the scenario when class invariants are very complex and hard to satisfy.
Let’s say you want to create triangle object, feeding it’s constructor with 3 angles.

Triangle(int angle1, int angle2, int angle3)
For triangle all interior angles total to 180 degrees.
This invariant check can be done in constructor itself or in Factory..

This approach seems to be very attractive – you will always work with valid triangles, but as i mentioned above it kills testability:
To test any of triangle behavior (for example checking if triangle is right-angled) you first need to instantiate it feeding it with correct angle values.
For triangle example it still does not seem to be the case, but in more complex scenarios instantiating object to be tested can be quite hard.

So having factories/constructor for tests can be quite tempting but how would you enforce your colleagues to not call them in their code ?
By writing appropriate comments (javadoc)?
By using access level modifiers?</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I also bumped into invariants checking in constructor/factory dilemma. </p>
<p>The main drawback of test-only constructors/factories is that you can not guarantee that none of your colleagues will use them in production code !!!</p>
<p>On the other side, calling production-available (real) constructors/factories in your test can be very painfull&#8230;</p>
<p>Invariant checks inside User.CreateUserFor(headers) are quite trivial so it does not seem to be the case  but imagine the scenario when class invariants are very complex and hard to satisfy.<br />
Let’s say you want to create triangle object, feeding it’s constructor with 3 angles.</p>
<p>Triangle(int angle1, int angle2, int angle3)<br />
For triangle all interior angles total to 180 degrees.<br />
This invariant check can be done in constructor itself or in Factory..</p>
<p>This approach seems to be very attractive – you will always work with valid triangles, but as i mentioned above it kills testability:<br />
To test any of triangle behavior (for example checking if triangle is right-angled) you first need to instantiate it feeding it with correct angle values.<br />
For triangle example it still does not seem to be the case, but in more complex scenarios instantiating object to be tested can be quite hard.</p>
<p>So having factories/constructor for tests can be quite tempting but how would you enforce your colleagues to not call them in their code ?<br />
By writing appropriate comments (javadoc)?<br />
By using access level modifiers?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Coding: API readability/testability at Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-23707</link>
		<dc:creator>Coding: API readability/testability at Mark Needham</dc:creator>
		<pubDate>Fri, 09 Oct 2009 14:25:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-23707</guid>
		<description>[...] a month ago or so I described how we did some work to ensure that we were calling a class the same way in our tests as... and while I think that was a good choice in that situation we came across a similar problem this [...]</description>
		<content:encoded><![CDATA[<p>[...] a month ago or so I described how we did some work to ensure that we were calling a class the same way in our tests as&#8230; and while I think that was a good choice in that situation we came across a similar problem this [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nat</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22804</link>
		<dc:creator>Nat</dc:creator>
		<pubDate>Sun, 20 Sep 2009 21:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22804</guid>
		<description>It looks to me as if your User class has too many responsibilities.  One is to define how User objects act (part of your application&#039;s domain model). The other is to parse header fields that have come from some message or HTTP request or somewhere and interpret how they are used to define a User.

If you move the latter responsibility out of the User and into a package of classes that define your app&#039;s network protocol (for example), then you only need one constructor for the User that is used by both the network protocol package and unit tests.</description>
		<content:encoded><![CDATA[<p>It looks to me as if your User class has too many responsibilities.  One is to define how User objects act (part of your application's domain model). The other is to parse header fields that have come from some message or HTTP request or somewhere and interpret how they are used to define a User.</p>
<p>If you move the latter responsibility out of the User and into a package of classes that define your app's network protocol (for example), then you only need one constructor for the User that is used by both the network protocol package and unit tests.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sai Venkat</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22554</link>
		<dc:creator>Sai Venkat</dc:creator>
		<pubDate>Mon, 14 Sep 2009 20:15:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22554</guid>
		<description>Wow... And I wonder why people want to try to use things like InternalsVisibleTo or reflections to test this? I think your solution is way more elegant that those :)</description>
		<content:encoded><![CDATA[<p>Wow&#8230; And I wonder why people want to try to use things like InternalsVisibleTo or reflections to test this? I think your solution is way more elegant that those <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: Sai Venkat</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22553</link>
		<dc:creator>Sai Venkat</dc:creator>
		<pubDate>Mon, 14 Sep 2009 20:12:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22553</guid>
		<description>Think I ran into the same situation when we were writing some Http Handlers based on Asp.Net in our project. In application, the query parameters are passed to a factory which creates object based on the request. In test, we used a QueryParametersBuilder which will construct this for us.
I think it is similar to your approach of having a UserBuilder which abstracts the process of creating the user from the request. It does leak some implementation but I think as @Dcam has pointed it the part of contract...</description>
		<content:encoded><![CDATA[<p>Think I ran into the same situation when we were writing some Http Handlers based on Asp.Net in our project. In application, the query parameters are passed to a factory which creates object based on the request. In test, we used a QueryParametersBuilder which will construct this for us.<br />
I think it is similar to your approach of having a UserBuilder which abstracts the process of creating the user from the request. It does leak some implementation but I think as @Dcam has pointed it the part of contract&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uday</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22494</link>
		<dc:creator>Uday</dc:creator>
		<pubDate>Sun, 13 Sep 2009 13:45:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22494</guid>
		<description>If it was a Java code, i would have preferred using reflection to create the User object in UserBuilder without changing anything in the current User class.

Not sure if it is feasible in C# (assuming the code you mentioned is written in C#)</description>
		<content:encoded><![CDATA[<p>If it was a Java code, i would have preferred using reflection to create the User object in UserBuilder without changing anything in the current User class.</p>
<p>Not sure if it is feasible in C# (assuming the code you mentioned is written in C#)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Darshan</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22453</link>
		<dc:creator>Darshan</dc:creator>
		<pubDate>Sat, 12 Sep 2009 03:49:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22453</guid>
		<description>You can make all your &quot;just for test&quot; methods internal and then access in your test code by using assembly:InternalsVisibleTo attribute.This is tedious but is better insulted then your solution.</description>
		<content:encoded><![CDATA[<p>You can make all your "just for test" methods internal and then access in your test code by using assembly:InternalsVisibleTo attribute.This is tedious but is better insulted then your solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Needham</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22452</link>
		<dc:creator>Mark Needham</dc:creator>
		<pubDate>Sat, 12 Sep 2009 03:46:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22452</guid>
		<description>@Robert - I&#039;d have said that hiding the creation behind the builder as you suggest in 2) would make it less necessary to have the test constructor?

I guess in our case we didn&#039;t have specific tests around the real constructor although it was being called in our acceptance tests so it was still being tested somewhere.</description>
		<content:encoded><![CDATA[<p>@Robert &#8211; I'd have said that hiding the creation behind the builder as you suggest in 2) would make it less necessary to have the test constructor?</p>
<p>I guess in our case we didn't have specific tests around the real constructor although it was being called in our acceptance tests so it was still being tested somewhere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Watkins</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22451</link>
		<dc:creator>Robert Watkins</dc:creator>
		<pubDate>Sat, 12 Sep 2009 03:37:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22451</guid>
		<description>Some thoughts:

1) There&#039;s nothing wrong with adding a test constructor - as long as you have tests that also test the real constructor. Some objects (particularly key domain concepts) get instantiated a lot by test code - making that easier and cleaner will help the _other_ tests.

2) You can help clean up the leaking by using a builder to create the name value collection. If the same builder is used in the production code, all the better.</description>
		<content:encoded><![CDATA[<p>Some thoughts:</p>
<p>1) There's nothing wrong with adding a test constructor &#8211; as long as you have tests that also test the real constructor. Some objects (particularly key domain concepts) get instantiated a lot by test code &#8211; making that easier and cleaner will help the _other_ tests.</p>
<p>2) You can help clean up the leaking by using a builder to create the name value collection. If the same builder is used in the production code, all the better.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ollie Riches</title>
		<link>http://www.markhneedham.com/blog/2009/09/12/tdd-test-only-constructors/comment-page-1/#comment-22442</link>
		<dc:creator>Ollie Riches</dc:creator>
		<pubDate>Fri, 11 Sep 2009 20:46:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1628#comment-22442</guid>
		<description>Surely adding anything to the &#039;production code&#039; for a testing purposes is a code smell at the minimum and in the worse case demonstrates that the code is not completely testable - can&#039;t be decoupled so therefore can&#039;t be tested...</description>
		<content:encoded><![CDATA[<p>Surely adding anything to the 'production code' for a testing purposes is a code smell at the minimum and in the worse case demonstrates that the code is not completely testable &#8211; can't be decoupled so therefore can't be tested&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
