<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>YTechie.com &#187; unit testing</title>
	<atom:link href="http://www.ytechie.com/category/unit-testing/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ytechie.com</link>
	<description>Productive software development using ASP.NET, C#, Adobe Flex, and other technologies and tools.</description>
	<lastBuildDate>Fri, 06 Nov 2009 21:16:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Practical .NET Unit Testing &#8211; Free paper released</title>
		<link>http://www.ytechie.com/2009/04/practical-net-unit-testing-free-paper-released.html</link>
		<comments>http://www.ytechie.com/2009/04/practical-net-unit-testing-free-paper-released.html#comments</comments>
		<pubDate>Thu, 30 Apr 2009 13:04:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[software development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2009/04/practical-net-unit-testing-free-paper-released.html</guid>
		<description><![CDATA[I’ve been working on a unit testing paper that sums up my experience in unit testing, and discusses some of the core information that I feel is important about the subject. It’s very much a work in progress, but I wanted to get it out sooner rather than later. I’ll be continuously updating it as [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been working on a unit testing paper that sums up my experience in unit testing, and discusses some of the core information that I feel is important about the subject. It’s very much a work in progress, but I wanted to get it out sooner rather than later. I’ll be continuously updating it as time goes on.</p>
<p><strong>Update: I updated the <a href="http://downloads.ytechie.com/Practical_.NET_Unit_Testing.pdf" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/downloads.ytechie.com/Practical_.NET_Unit_Testing.pdf?referer=');">PDF location</a> to one that doesn&#8217;t require registration.</strong></p>
<p align="center"><a href="http://downloads.ytechie.com/Practical_.NET_Unit_Testing.pdf" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/downloads.ytechie.com/Practical_.NET_Unit_Testing.pdf?referer=');"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Practical .NET Unit Testing" border="0" alt="Practical .NET Unit Testing" src="http://www.ytechie.com/post-images/2009/05/image.png" width="392" height="203" /></a></p>
<p align="left">There are some really great books out there about unit testing, but I think some of them are trying too hard to be long enough to be considered a “book”. I set out to create a document that fills the gap between the various snippets of information from blog posts, and the comprehensive books on the subject. If you’re interested in something a bit more in-depth, here are some great books on the subject:</p>
<ul>
<li>
<div><a href="http://www.amazon.com/gp/product/1933988274?ie=UTF8&amp;tag=ytechie-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=1933988274" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/gp/product/1933988274?ie=UTF8_amp_tag=ytechie-20_amp_linkCode=xm2_amp_camp=1789_amp_creativeASIN=1933988274&amp;referer=');">The Art of Unit Testing by Roy Osherove</a></div>
</li>
<li>
<div><a href="http://www.amazon.com/gp/product/0131495054?ie=UTF8&amp;tag=ytechie-20&amp;linkCode=xm2&amp;camp=1789&amp;creativeASIN=0131495054" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/gp/product/0131495054?ie=UTF8_amp_tag=ytechie-20_amp_linkCode=xm2_amp_camp=1789_amp_creativeASIN=0131495054&amp;referer=');">xUnit Test Patterns by Gerard Meszaros</a></div>
</li>
</ul>
<p>The paper currently consists of 5 main sections:</p>
<ul>
<li>Why Write Unit Tests? </li>
<li>Unit Test Mechanics </li>
<li>Common Unit Testing Strategies </li>
<li>Designing for Testability </li>
<li>Advanced Techniques </li>
</ul>
<p>Here is a more complete snapshot of the current outline:</p>
<ul>
<li>Introduction </li>
<li>Unit Testing &amp; Managers </li>
<li>What Unit Tests Really Do </li>
<li>Types of Testing </li>
<li>Testing Framework </li>
<li>Test Runner </li>
<li>Unit Test Structure </li>
<li>Other Test Attributes </li>
<li>What is Refactoring? </li>
<li>Test Driven Development </li>
<li>Evolving Code </li>
<li>When Should You Write Unit Tests? </li>
<li>Test is for Functionality, Not Code! </li>
<li>The Constraints of Reality </li>
<li>Interfaces &#8211; Quick Overview </li>
<li>Using a Mocking Framework </li>
<li>Stubs </li>
<li>The Test Driven Design Paradox </li>
<li>Testing Under Pressure </li>
<li>Extracting Duplicate Logic </li>
<li>Modular Design Benefits </li>
</ul>
<p>So what are you waiting for? <a href="http://www.scribd.com/doc/14713003/Practical-NET-Unit-Testing" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.scribd.com/doc/14713003/Practical-NET-Unit-Testing?referer=');">Go check it out online instantly</a>, you can even <a href="http://downloads.ytechie.com/Practical_.NET_Unit_Testing.pdf" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/downloads.ytechie.com/Practical_.NET_Unit_Testing.pdf?referer=');">download it as a PDF</a> if you like. Is anything missing? Is anything just plain wrong? I’d love to hear your feedback.</p>
<p>Remember, if you want to hear more about unit testing, <a href="http://www.ytechie.com/2009/04/speaking-at-day-of-net-at-fox-valley-tech.html">I’ll be speaking in Northeast Wisconsin Saturday, May 9th</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2009/04/practical-net-unit-testing-free-paper-released.html/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Speaking at Day of .NET at Fox Valley Tech</title>
		<link>http://www.ytechie.com/2009/04/speaking-at-day-of-net-at-fox-valley-tech.html</link>
		<comments>http://www.ytechie.com/2009/04/speaking-at-day-of-net-at-fox-valley-tech.html#comments</comments>
		<pubDate>Tue, 28 Apr 2009 14:54:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2009/04/speaking-at-day-of-net-at-fox-valley-tech.html</guid>
		<description><![CDATA[If you’re interested in hearing about writing practical unit tests in .NET, I’ll be speaking at the Fox Valley .NET user group “Day of .NET” event May 9th! Here is the synopsis for your reading pleasure:
Want to learn how to write good automated unit tests that are beneficial both to the product/customer and to you [...]]]></description>
			<content:encoded><![CDATA[<p>If you’re interested in hearing about writing practical unit tests in .NET, I’ll be speaking at the <a href="http://fvnug.org" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/fvnug.org?referer=');">Fox Valley .NET user group</a> <a href="http://fvnug.org/dnn/DayOfNet/Schedule/tabid/62/Default.aspx" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/fvnug.org/dnn/DayOfNet/Schedule/tabid/62/Default.aspx?referer=');">“Day of .NET” event</a> May 9th! Here is the synopsis for your reading pleasure:</p>
<blockquote><p>Want to learn how to write good automated unit tests that are beneficial both to the product/customer and to you as a developer? See an overview of the mechanics of unit testing including the tools and frameworks available. You&#8217;ll see examples of how to test existing code, but you&#8217;ll also see practical examples of how seemingly un-testable code can be designed so that it can be tested with ease. Learn how test driven development and refactoring will improve the readability of your code, minimize debugging, and speed up development.</p>
</blockquote>
<p><a href="http://fvnug.org" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/fvnug.org?referer=');"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.ytechie.com/post-images/2009/04/image5.png" width="490" height="79" /></a> </p>
<p>If you’re anywhere near the Northeast Wisconsin area, stop in. It’s free!</p>
<p>I’ll be publishing both the presentation and a supporting 25+ page paper shortly, so make sure you’re <a href="http://feedproxy.google.com/Ytechie" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/feedproxy.google.com/Ytechie?referer=');">subscribed to my feed</a>.</p>
<p><a href="http://www.fvtc.edu/public/" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.fvtc.edu/public/?referer=');">Fox Valley Tech</a> is located at:     <br /><a href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=1825+N.+Bluemound+Drive,+Appleton+WI&amp;sll=37.0625,-95.677068&amp;sspn=1.468445,2.771301&amp;ie=UTF8&amp;z=15&amp;msa=0&amp;msid=107741674408312530799.000001120068a94c2e438" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/maps.google.com/maps?f=q_amp_source=s_q_amp_hl=en_amp_geocode=_amp_q=1825+N.+Bluemound+Drive_+Appleton+WI_amp_sll=37.0625_-95.677068_amp_sspn=1.468445_2.771301_amp_ie=UTF8_amp_z=15_amp_msa=0_amp_msid=107741674408312530799.000001120068a94c2e438&amp;referer=');">1825 N. Bluemound</a>     <br />Appleton, WI 54912</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2009/04/speaking-at-day-of-net-at-fox-valley-tech.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Testing a LINQ to SQL or EF Query</title>
		<link>http://www.ytechie.com/2009/04/unit-testing-a-linq-to-sql-or-ef-query.html</link>
		<comments>http://www.ytechie.com/2009/04/unit-testing-a-linq-to-sql-or-ef-query.html#comments</comments>
		<pubDate>Thu, 09 Apr 2009 18:58:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2009/04/unit-testing-a-linq-to-sql-or-ef-query.html</guid>
		<description><![CDATA[I was writing a slightly non-trivial method to query a database to find a record matching a certain time range. It quickly became clear that it would be nice to write some automated unit tests against it. Integration tests would be less than ideal because of the execution time and complexity. I ended up with [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing a slightly non-trivial method to query a database to find a record matching a certain time range. It quickly became clear that it would be nice to write some automated unit tests against it. Integration tests would be less than ideal because of the execution time and complexity. I ended up with a way to test the code without jumping through too many hoops.</p>
<p><strong>IEnumerable vs IQueryable</strong></p>
<p>First, you need to understand the purpose of IEnumerable and IQueryable. IEnumerable defines a stream of objects that can be retrieved sequentially. It’s implemented for nearly every type of list, and it’s an integral part of LINQ. There are now methods included such as “Where” and “Select” that let us filter, sort, and manipulate lists of data in interesting yet simple ways. This can also be referred to as LINQ to objects.</p>
<p>IQueryable inherits from IEnumerable, and is designed to be translatable into a query. IQueryable is typically used to build an expression tree that <strong>represents</strong> the requested operations. The operations are not actually executed until the expression tree is evaluated and used.</p>
<p>As an example, let’s say I have a database with a table and entities called DateRange. Suppose I cast the DateRange entityset (which implements IQueryable&lt;DateRange&gt;) as IEnumerable&lt;DateRange&gt;. When I call LINQ expressions on that IEnumerable, the underlying query is run immediately, which effectively causes all of the data from that table to be retrieved. If I use IQueryable without casting, my operations get turned into SQL that gets executed when I actually try to iterate through the data (probably using ToList() or foreach). It’s obviously preferable to have the query run in SQL since it can more efficiently filter the data.</p>
<p><strong>The Problem</strong></p>
<p>As I mentioned earlier, I recently starting writing a data access method that started to contain some non-trivial logic. Whenever I see logic, I want to be able to unit test it! I ended up pulling out the logic into its own static method. This method takes in IEnumerable&lt;DateRange&gt;, which can also accept IQueryable&lt;DateRange&gt; (because of the inheritance, you’ll recall). Then, I simply use the AsQueryable method in IEnumerable. This ends up building the needed expression tree that can be translated into a SQL query, but it also lets me test against an in memory collection.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:fa00aac0-6c39-4e4b-bc9f-a7781d500240" class="wlWriterEditableSmartContent">
<pre name="code" class="c#">public static DateRange FindRelativeToDate(IEnumerable&lt;DateRange&gt; enumerable, DateTime reference, int periodOffset)
{
	//Build as an expression tree, if possible, otherwise enumerate
	var queryable = enumerable.AsQueryable();

	//Now put in all the logic

	var reference2 = reference.AddDays(-1);

	var initialRange = from bp in queryable
		 where reference &gt;= bp.Start &amp;&amp; reference2 &lt; bp.End
		 select bp;

	var currentDateRange = initialRange.First();

	if(periodOffset == 0)
		return currentDateRange;

	var newRange = from bp in queryable
		select bp;

	if (periodOffset &gt; 0)
	{
		newRange = newRange.Where(x =&gt; x.Start &gt;= currentDateRange.Start);
		newRange = newRange.OrderBy(x =&gt; x.Start);
	}
	else
	{
		newRange = newRange.Where(x =&gt; x.End &lt;= currentDateRange.End);
		newRange = newRange.OrderByDescending(x =&gt; x.Start);
	}

	return newRange.Skip(Math.Abs(periodOffset)).Take(1).FirstOrDefault();
}</pre>
</div>
<p>In my unit test class, I can define a list of sample data. I took real data from the database to make the tests as close to reality as possible:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:04d1e3b3-50b1-48e5-931a-eb3c96fa191e" class="wlWriterEditableSmartContent">
<pre name="code" class="c#">private static readonly List&lt;DateRange&gt; _DateRanges = new List&lt;DateRange&gt;
{
	new DateRange {Start = new DateTime(2009, 1, 1), End = new DateTime(2009, 1, 30)},
					new DateRange {Start = new DateTime(2009, 1, 31), End = new DateTime(2009, 3, 01)},
					new DateRange {Start = new DateTime(2009, 3, 2), End = new DateTime(2009, 3, 31)},
					new DateRange {Start = new DateTime(2009, 4, 1), End = new DateTime(2009, 4, 30)},
};</pre>
</div>
<p>Now, I can easily test the static class I wrote (this is 1 of 9+ real tests):</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:44884bb7-a315-4cd2-a693-f699e9d4e34a" class="wlWriterEditableSmartContent">
<pre name="code" class="c#">[TestMethod]
public void FindRelativeToDate_MiddleOfDateRange_ContainingDateRange()
{
	var result = DateRangeRepository.FindRelativeToDate(_DateRanges, new DateTime(2009, 3, 15), 0);
	Assert.AreEqual(_DateRanges[2], result);
}</pre>
</div>
<p>The only thing that is left to do is wire up the repository method so that it calls my static method. This is a thin wrapper layer that will actually get used in production. If you run profiler, you’ll see that the query expression is being evaluated and converted into an efficient SQL expression.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:ad5a6773-4fc7-435a-b3f1-f68cc877ff64" class="wlWriterEditableSmartContent">
<pre name="code" class="c#">public DateRange FindRelativeToDate(DateTime reference, int periodOffset)
{
	var ctx = dbEntities;
	return FindRelativeToDate(ctx.DateRangeSet, reference, periodOffset);
}</pre>
</div>
<p><strong>Limitations/Conclusion</strong></p>
<p>Unfortunately, this method of testing a repository doesn’t scale easily. If you start working with multiple entity sets that are combined with join operations, this technique is next to impossible to use. You’ll see the most benefit when working with a single entity type, and need to test logic in your repository method.</p>
<p>One thing you need to be aware of, is that LINQ to SQL and Entity Framework don’t implement every IQueryable/IEnumerable method. This means that you could potentially make calls on the in-memory collection that will then fail when you use the actual database. Fortunately, these problems can usually be detected fairly quickly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2009/04/unit-testing-a-linq-to-sql-or-ef-query.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Screencast &#8211; Unit testing with real DataSets</title>
		<link>http://www.ytechie.com/2008/09/screencast-unit-testing-with-real-datasets.html</link>
		<comments>http://www.ytechie.com/2008/09/screencast-unit-testing-with-real-datasets.html#comments</comments>
		<pubDate>Mon, 22 Sep 2008 20:01:53 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/09/screencast-unit-testing-with-real-datasets.html</guid>
		<description><![CDATA[As I&#8217;ve mentioned before, I wrote a small utility for running a SQL query, and returning the results as an XML serilialized DataSet.
I created a quick screencast to demonstrate how you go about using this utility. I&#8217;m new to screencasts, so don&#8217;t expect a high production value!
 
You can check out the screencast here!
Let me [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ytechie.com/2008/04/net-xml-dataset-generator-from-a-sql-query.html">As I&#8217;ve mentioned before</a>, I <a href="http://code.google.com/p/queryxmldataset/" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/queryxmldataset/?referer=');">wrote a small utility</a> for running a SQL query, and returning the results as an XML serilialized DataSet.</p>
<p>I created a quick screencast to demonstrate how you go about using this utility. I&#8217;m new to screencasts, so don&#8217;t expect a high production value!</p>
<p align="center"><a href="http://s3.ytechie.com/screencasts/QueryXmlDataSet-Screencast/QueryXmlDataSet-Screencast.htm" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/s3.ytechie.com/screencasts/QueryXmlDataSet-Screencast/QueryXmlDataSet-Screencast.htm?referer=');"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="379" alt="image" src="http://www.ytechie.com/post-images/2008/09/image2.png" width="407" border="0" /></a> </p>
<p align="center"><strong><a href="http://s3.ytechie.com/screencasts/QueryXmlDataSet-Screencast/QueryXmlDataSet-Screencast.htm" target="_blank" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/s3.ytechie.com/screencasts/QueryXmlDataSet-Screencast/QueryXmlDataSet-Screencast.htm?referer=');">You can check out the screencast here!</a></strong></p>
<p align="left">Let me know what you think in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/09/screencast-unit-testing-with-real-datasets.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Design your classes for their consumer</title>
		<link>http://www.ytechie.com/2008/08/design-your-classes-for-their-consumer.html</link>
		<comments>http://www.ytechie.com/2008/08/design-your-classes-for-their-consumer.html#comments</comments>
		<pubDate>Mon, 11 Aug 2008 19:44:30 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[productivity]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/08/design-your-classes-for-their-consumer.html</guid>
		<description><![CDATA[I&#8217;m going to describe a methodology that will help you save time by writing better classes, and will help simplify your life by allowing you to solve problems with a top-down approach.
Developers such as myself often have a tendency to just focus on the class we&#8217;re currently working on. Of course I believe this is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m going to describe a methodology that will help you save time by writing better classes, and will help simplify your life by allowing you to solve problems with a top-down approach.</p>
<p>Developers such as myself often have a tendency to just focus on the class we&#8217;re currently working on. Of course I believe <a href="http://www.ytechie.com/2008/07/programming-for-someone-with-blinders.html">this is a good thing</a>, because we all know <a href="http://www.ytechie.com/2008/05/focus-is-the-key-to-success.html">the importance of focus</a>. However, you should never forget the reason you&#8217;re actually writing that class. It is because <strong>other code will be consuming it</strong>.</p>
<p><a href="http://www.ytechie.com/post-images/2008/08/consumer-approach.gif"><img height="76" alt="Consumer-Approach" src="http://www.ytechie.com/post-images/2008/08/consumer-approach-thumb.gif" width="484" border="0"/></a> </p>
<p>There have been far too many instances where I would figure out which pieces I needed to build, and then build each one, from the bottom up. The problem is that I would write the class in the most short-sighted and <strong>easiest</strong> way possible, which is usually not the best way to use it.</p>
<p>Now, when there is a question of what a class interface should look like, I ask myself what it <em>should</em> look like to make the life of the consumer as easy as possible.</p>
<p>Sometimes we can even take code usability to an extreme. For example, <a href="http://en.wikipedia.org/wiki/Fluent_interface" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Fluent_interface?referer=');">Fluent interfaces</a>, which allow you to chain together multiple calls. In many instances, this makes the code much easier to <em>call</em>, potentially at the expense of making the <em>called</em> code more complicated.</p>
<p>I&#8217;ve come up with a simple example to help illustrate. Suppose I need to process a list of x,y coordinates. Here are a couple of potential signatures:</p>
<ul>
<li>ProcessData(double[] xData, double[] yData);</li>
<li>ProcessData(PointF[] points);</li>
<li>ProcessData(IDictionary&lt;double, double&gt; points);</li>
</ul>
<p>For now, just ignore the performance implications (<a href="http://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Big_O_notation_Orders_of_common_functions?referer=');">they&#8217;re going to be linear in this case</a>, or close to it anyway). To choose the correct signature, we need to know who the caller is.</p>
<p>Of course there is a good chance that we&#8217;ll have a slight intentional leaky abstraction. In the previous example, we may have been able to use the <a href="http://msdn.microsoft.com/en-us/library/9eekhta0.aspx" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/9eekhta0.aspx?referer=');">IEnumerable generic</a> to be more flexible. That&#8217;s a topic for another day.</p>
<p>Thankfully, this problem is minimized, although not always eliminated when you follow the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Single_responsibility_principle?referer=');">single responsibility principle</a>. The better you can follow that principle, the simpler each piece will be. That tends to minimize the potential for the consumer to need the interface to look different than it would naturally be.</p>
<p>Another way to look at your classes from the consumers point of view is to practice test-driven development. In fact, I see this as one of the strongest arguments for test driven design. For each layer in your code, you&#8217;re creating code by consuming it before writing it. Every layer acts as an API to the layer above it.</p>
<p>In conclusion, I&#8217;m simply recommending that you don&#8217;t lose sight of <strong>why</strong> you&#8217;re writing that piece of code. You&#8217;re not just writing code for the sake of writing code, you&#8217;re writing it to be used!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/08/design-your-classes-for-their-consumer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Dependency Injection example with Spring.NET</title>
		<link>http://www.ytechie.com/2008/06/a-dependency-injection-example-with-springnet.html</link>
		<comments>http://www.ytechie.com/2008/06/a-dependency-injection-example-with-springnet.html#comments</comments>
		<pubDate>Fri, 13 Jun 2008 13:32:51 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/06/a-dependency-injection-example-with-springnet.html</guid>
		<description><![CDATA[As requested, here is a real world example of how I used dependency injection to simplify a project, increase modularity, and subsequently increase testability.
Here&#8217;s the project. I have a successful website called SimpleTracking.com which allows you to track packages using a simple, common user interface. It also allows you to track pages using RSS.
Here is [...]]]></description>
			<content:encoded><![CDATA[<p>As requested, here is a real world example of how I used <a href="http://www.ytechie.com/2008/06/i-finally-get-the-point-of-inversion-of-control.html">dependency injection</a> to simplify a project, increase modularity, and subsequently increase testability.</p>
<p>Here&#8217;s the project. I have a successful website called <a href="http://www.SimpleTracking.com" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.SimpleTracking.com?referer=');">SimpleTracking.com</a> which allows you to <a href="http://www.SimpleTracking.com" onclick="pageTracker._trackPageview('/outgoing/www.SimpleTracking.com?referer=');">track packages</a> using a simple, common user interface. It also allows you to track pages using RSS.</p>
<p>Here is a list of features:</p>
<ul>
<li>Supports multiple shippers, including FedEx, DHL, and USPS. The tracking number is resolved to one of them, and if a tracking number could belong to more than one, they are called simultaneously, and the one that returns the results is used. </li>
<li>Results are cached to avoid overusing the shippers servers </li>
<li>Errors are handled appropriately </li>
</ul>
<p>I boiled the design into a tree of classes:</p>
<p align="center">&#160;<img height="361" alt="image" src="http://www.ytechie.com/post-images/2008/06/image1.png" width="293" border="0" /> </p>
<p>To greatly simply the design, I decided that each module would implement a common interface. After all, they all take in a tracking number, and return tracking data. Here is the ITracker interface:</p>
<pre class="c-sharp" name="code">public interface ITracker
{
	TrackingData GetTrackingData(string trackingNumber);
}</pre>
<p>Simple enough? <strong>Every </strong>class in the diagram above implements the same interface. If I want to add additional functionality, such as logging for example, I can simply add a class to the chain, and implement the same interface.</p>
<p>Now I can wire it up with Spring.NET:</p>
<pre class="xml" name="code">&lt;object name=&quot;postUtility&quot; type=&quot;YTech.ShipperInterface.Tracking.Http.PostUtility, YTech.ShipperInterface&quot; /&gt;

&lt;!-- The trackers that actually do the work --&gt;
&lt;object name=&quot;uspsTracker&quot; type=&quot;YTech.ShipperInterface.Usps.Tracking.UspsTracker, YTech.ShipperInterface&quot;&gt;
	&lt;constructor-arg ref=&quot;postUtility&quot; /&gt;
	&lt;!-- Code removed for readability... --&gt;
&lt;/object&gt;
&lt;object name=&quot;fedexTracker&quot; type=&quot;YTech.ShipperInterface.FedEx.Tracking.FedexTracker, YTech.ShipperInterface&quot;&gt;
	&lt;constructor-arg ref=&quot;postUtility&quot; /&gt;
	&lt;!-- Code removed for readability... --&gt;
&lt;/object&gt;
&lt;object name=&quot;dhlTracker&quot; type=&quot;YTech.ShipperInterface.Dhl.Tracking.DhlScreenScrapeTracker, YTech.ShipperInterface&quot;&gt;
	&lt;constructor-arg ref=&quot;postUtility&quot; /&gt;
&lt;/object&gt;
&lt;object name=&quot;simulationTracker&quot; type=&quot;YTech.ShipperInterface.Tracking.Simulation.SimulationTracker, YTech.ShipperInterface&quot; /&gt;

&lt;!-- Combine all of the other trackers into one stream --&gt;
&lt;object name=&quot;multiTracker&quot; type=&quot;YTech.ShipperInterface.Tracking.MultiTracker, YTech.ShipperInterface&quot;&gt;
	&lt;constructor-arg&gt;
		&lt;list element-type=&quot;YTech.ShipperInterface.Tracking.ITracker, YTech.ShipperInterface&quot;&gt;
			&lt;ref object=&quot;simulationTracker&quot; /&gt;
			&lt;!-- Order these by popularity --&gt;
			&lt;ref object=&quot;fedexTracker&quot; /&gt;
			&lt;ref object=&quot;uspsTracker&quot; /&gt;
			&lt;ref object=&quot;dhlTracker&quot; /&gt;
		&lt;/list&gt;
	&lt;/constructor-arg&gt;
&lt;/object&gt;

&lt;!-- Cache the upstream tracking data --&gt;
&lt;object name=&quot;cacheTracker&quot; type=&quot;YTech.ShipperInterface.Tracking.CacheTracker, YTech.ShipperInterface&quot;&gt;
	&lt;constructor-arg ref=&quot;multiTracker&quot; /&gt;
&lt;/object&gt;

&lt;!-- Handle errors by logging them, and returning a special ErrorTrackingData object --&gt;
&lt;object name=&quot;MainTracker&quot; type=&quot;YTech.ShipperInterface.Tracking.ErrorHandlerTracker, YTech.ShipperInterface&quot;&gt;
	&lt;constructor-arg ref=&quot;cacheTracker&quot; /&gt;
&lt;/object&gt;</pre>
<p>Now in my code, this is all I have to do:</p>
<pre class="c-sharp" name="code">var ctx = ContextRegistry.GetContext();
var tracker = (ITracker)ctx.GetObject(&quot;MainTracker&quot;);
var td = tracker.GetTrackingData(&quot;my tracking number&quot;);</pre>
<p>Every piece I&#8217;ve written is fully testable. I even created a class that posts data to a remote web server, and returns the response. This allows me to completely test the tracker classes. They don&#8217;t care if they&#8217;re hitting against a real server, or an in memory request/response mock class.</p>
<p>I have nearly 100% test coverage, and making changes to the site is a breeze.</p>
<p>The next step is to convert the actual web project to an MVC project so that I can unit test the actual page functionality.</p>
<p>Hopefully I&#8217;ve given a good example of how inversion of control can be a really good thing. Have any more questions about the architecture? Feel free to leave a comment.</p>
<p>Note: I haven&#8217;t replaced the code on the live SimpleTracking.com site yet, but I plan on upgrading in the next couple of weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/06/a-dependency-injection-example-with-springnet.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Unit tests are for functionality, not code!</title>
		<link>http://www.ytechie.com/2008/06/unit-tests-are-for-functionality-not-code.html</link>
		<comments>http://www.ytechie.com/2008/06/unit-tests-are-for-functionality-not-code.html#comments</comments>
		<pubDate>Thu, 12 Jun 2008 12:13:16 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/06/unit-tests-are-for-functionality-not-code.html</guid>
		<description><![CDATA[Shawn has an interesting post where he talks about why 100% unit test coverage should be one of your goals. I agree 100%. I&#8217;m not sure why anyone would say that you shouldn&#8217;t be testing your properties. Don&#8217;t you want to make sure they work?
 
The prevailing philosophy in regards to unit testing is writing [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.obishawn.com/" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/blog.obishawn.com/?referer=');">Shawn</a> has an interesting post where he talks about <a href="http://blog.obishawn.com/2008/06/why-you-should-have-100-code-test.html" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/blog.obishawn.com/2008/06/why-you-should-have-100-code-test.html?referer=');">why 100% unit test coverage should be one of your goals</a>. I agree 100%. I&#8217;m not sure why anyone would say that you shouldn&#8217;t be testing your properties. Don&#8217;t you want to make sure they work?</p>
<p align="center"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="170" alt="Unit-Testing" src="http://www.ytechie.com/post-images/2008/10/unit-testing.jpg" width="254" border="0" /> </p>
<p>The prevailing philosophy in regards to unit testing is writing your tests before your code. In practice, this happens a lot less than it should. Why <em>should</em> we write our unit tests first?</p>
<ul>
<li>Writing your unit tests first makes you <strong>design pieces of your software from the clients perspective</strong>. After all, you&#8217;re writing your code to be consumed. The code itself is not important, it&#8217;s what it does for other code. </li>
<li><strong>Unit tests are meant to test functionality, NOT code!</strong> That means if you write your unit tests after the fact, you&#8217;re probably not focusing on the functionality. You might be trying to come up with edge cases that might not even matter to the client. It&#8217;s harder to come up with good unit tests after the code is written, because you&#8217;re not necessarily looking at it from the clients perspective, or from the perspective of the required functionality. </li>
</ul>
<p>There are two valid ways to increase code coverage:</p>
<ul>
<li><strong>Write additional tests</strong> &#8211; This only makes sense if you forgot to write the test initially. Since you&#8217;re tests are verifying functionality, why don&#8217;t you already have a test for this particular piece of functionality? </li>
<li><strong>Remove the untested code</strong> &#8211; In a perfect world, this is what you would always do. After all, your tests verify that your code has a certain set of functionality. If you have untested code, that code isn&#8217;t needed. Why keep code you don&#8217;t need? </li>
</ul>
<p>Here is the basic process I recommend:</p>
<ol>
<li>Come up with a rough design in your head </li>
<li>Write unit tests to test a required piece of functionality </li>
<li>Write the code to provide that functionality </li>
<li>Verify that the unit tests pass, fix the code as necessary </li>
<li>Refactor as necessary </li>
<li>Run a code coverage tool, and get as close to 100% as possible using one of the methods I mentioned previously. </li>
<li>Go to #2 and repeat as necessary </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/06/unit-tests-are-for-functionality-not-code.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Do you really need a data access layer with LINQ?</title>
		<link>http://www.ytechie.com/2008/05/do-you-really-need-a-data-access-layer-with-linq.html</link>
		<comments>http://www.ytechie.com/2008/05/do-you-really-need-a-data-access-layer-with-linq.html#comments</comments>
		<pubDate>Wed, 07 May 2008 10:00:15 +0000</pubDate>
		<dc:creator>superjason</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/05/do-you-really-need-a-data-access-layer-with-linq.html</guid>
		<description><![CDATA[Lately I&#8217;ve been giving a lot of thought to using LINQ to access my database instead of using NHibernate. I&#8217;ve been a little confused as to how LINQ would work in a data access layer, but I&#8217;m starting to think it makes sense as a replacement to the data access layer.
This article was inspired by [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been giving a lot of thought to using <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/netframework/aa904594.aspx?referer=');">LINQ</a> to access my database instead of using NHibernate. I&#8217;ve been a little confused as to how LINQ would work in a data access layer, but I&#8217;m starting to think it makes sense as a <strong>replacement</strong> to the data access layer.</p>
<p>This article was inspired by the <a href="http://www.djangoproject.com/" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.djangoproject.com/?referer=');">Google App Engine</a>. Using the <a href="http://www.djangoproject.com/" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.djangoproject.com/?referer=');">Django framework</a> for database access is stupidly simple. They&#8217;re able to focus on getting something done, which is good enough in a lot of cases. Not every project has to be an N-Tier enterprise application.</p>
<p>Primarily, I write eCommerce websites. Consider the following diagram, which gives you a rough idea of how I&#8217;ve traditionally structured my applications:</p>
<p align="center"><img height="244" alt="Traditional Architecture" src="http://www.ytechie.com/post-images/2008/05/traditional-architecture.gif" width="287" border="0" /> </p>
<p>Notice that a lot of code is being tested. The sweet side of me likes unit tests, but the wheat part of me is telling me to simplify code when possible to minimize the need for unit tests. I&#8217;ve traditionally wanted a lot of unit tests at the data access layer because it tends to be a source of a lot of issues due to it&#8217;s complexity. It&#8217;s complexity is a result of <a href="http://en.wikipedia.org/wiki/Object-Relational_impedance_mismatch" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Object-Relational_impedance_mismatch?referer=');">impedance mismatch</a>.</p>
<p>Code that is a good candidate for unit testing:</p>
<ul>
<li>Logic/utility/static functions</li>
<li>Code that will be used in a lot of places, and has a well defined contract</li>
<li>Code that needs the highest levels of reliability</li>
</ul>
<p>Now consider what happens when we use LINQ and the <a href="http://msdn.microsoft.com/en-us/library/aa697427(vs.80).aspx" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/aa697427_vs.80_.aspx?referer=');">ADO.NET Entity Framework</a>. The impendence mismatch has been minimized because the entity framework has automatically written our model code to match the database. It has also been my experience that the queries that are not trivial are usually not re-used. In other words, <strong>query complexity is inversely proportional to the frequency of re-use</strong>.</p>
<p>The end result is that our code has been greatly simplified. Now our unit tests can focus on the business logic. We&#8217;ll actually have more time for unit testing, which should lead to more stable code where it is needed most.</p>
<p>Sure, our UI will contain what are basically database queries. The fact is that many pages simply need a specific set of data that will populate a drop down list for example. I know that it might give people a bad feeling (as it does to myself), but I think it&#8217;s something we need to get over. If you want to test the page, in most cases you can just run it, and see that it&#8217;s working. In conjunction with a good web testing tool, it would be easy to have high levels of reliability.</p>
<p align="center"><img height="244" alt="Linq Architecture" src="http://www.ytechie.com/post-images/2008/05/linq-architecture.gif" width="275" border="0" /> </p>
<p align="left">Obviously there are a lot of places where this is a bad idea. I&#8217;m certainly not condoning a complete lack of a data access layer for every application. I&#8217;m applying the 80/20 rule. Places it might not make sense, or where it&#8217;s gray:</p>
<ul>
<li>
<div align="left">A logic class that needs to make frequent, repeated database calls.</div>
</li>
<li>
<div align="left">An application that requires a certain level of testability.</div>
</li>
<li>
<div align="left">An application that contains some extremely common queries that may or may not be trivial.</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/05/do-you-really-need-a-data-access-layer-with-linq.html/feed</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>.NET XML DataSet Generator from a SQL Query</title>
		<link>http://www.ytechie.com/2008/04/net-xml-dataset-generator-from-a-sql-query.html</link>
		<comments>http://www.ytechie.com/2008/04/net-xml-dataset-generator-from-a-sql-query.html#comments</comments>
		<pubDate>Mon, 28 Apr 2008 03:01:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.ytechie.com/2008/04/net-xml-dataset-generator-from-a-sql-query.html</guid>
		<description><![CDATA[As promised, I&#8217;ve now made the .NET XML DataSet Generator available on Google Code.
 
If anyone can think of a better name for the project, please let me know! It&#8217;s really hard to convey what it actually does. On the project page, I put these basic steps, which should help make it clear:

Run this program [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.ytechie.com/2008/04/dataset-xml-serializer-utility-for-unit-testing.html">As promised</a>, I&#8217;ve now made the <a href="http://code.google.com/p/queryxmldataset/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/queryxmldataset/?referer=');">.NET XML DataSet Generator</a> available on Google Code.</p>
<p align="center"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="379" alt=".NET XML DataSet Generator" src="http://www.ytechie.com/post-images/2008/04/image1.png" width="407" border="0" /> </p>
<p align="left">If anyone can think of a better name for the project, please let me know! It&#8217;s really hard to convey what it actually does. On the project page, I put these basic steps, which should help make it clear:</p>
<ol>
<li>Run this program to execute a SQL statement</li>
<li>Copy the resulting XML into a file in your .NET unit test project</li>
<li>Embed the XML file you created</li>
<li>In your test code, load the embedded XML, deserialize it back into a DataSet (code coming soon)</li>
</ol>
<p>As the project matures a little bit, I&#8217;ll have better documentation. I may even whip up a video demonstration. It&#8217;s not really a fancy tool, but it does come in handy.</p>
<p>For example, let&#8217;s say you have a database method that returns a DataSet containing a table of blog post items. You have another method that turns that DataSet into an array of post objects. I know that you&#8217;re thinking you should normally use an <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Object-relational_mapping?referer=');">OR mapper</a>, but this is just an example, so please don&#8217;t interrupt me!. This tool lets you create a serialized DataSet that you can use in a unit test, which will reconstitute the DataSet into the exact same thing your database method would have returned. You can now pass in a snapshot of real data into the method your testing. If you ARE using an OR mapper like <a href="http://www.hibernate.org/343.html" rel="nofollow" onclick="pageTracker._trackPageview('/outgoing/www.hibernate.org/343.html?referer=');">NHibernate</a>, this probably won&#8217;t happen much, but when it does, this tool will come in handy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/04/net-xml-dataset-generator-from-a-sql-query.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DataSet XML serializer utility for unit testing</title>
		<link>http://www.ytechie.com/2008/04/dataset-xml-serializer-utility-for-unit-testing.html</link>
		<comments>http://www.ytechie.com/2008/04/dataset-xml-serializer-utility-for-unit-testing.html#comments</comments>
		<pubDate>Fri, 11 Apr 2008 12:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://207.36.235.13/2008/04/dataset-xml-serializer-utility-for-unit-testing.html</guid>
		<description><![CDATA[Quite a while ago I wanted to do some unit testing with DataSet&#8217;s and DataTable&#8217;s as input. In code, there isn&#8217;t really an easy way to generate them, especially if you want them to be a realistic subset of the real data in your database.
My solution was to build a program that acted like SQL [...]]]></description>
			<content:encoded><![CDATA[<p>Quite a while ago I wanted to do some unit testing with DataSet&#8217;s and DataTable&#8217;s as input. In code, there isn&#8217;t really an <em>easy</em> way to generate them, especially if you want them to be a realistic subset of the real data in your database.</p>
<p>My solution was to build a program that acted like SQL Server&#8217;s Management Studio, but who&#8217;s output is a serialized DataSet. The resulting XML can simply be embedded into your unit test project. In your unit test, you simply deserialize the DataSet XML into a DataSet.</p>
<p><img alt="DataSet XML Serializer" src="/post-images/2008/04/dataset-builder.png" border="0" height="375" width="403" /> </p>
<p>Is anyone interested in me posting the source code and compiled EXE? I&#8217;ll have to clean it up a bit, so I wanted to see if there was enough interest, of if it&#8217;s something I&#8217;ll just keep using in-house.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ytechie.com/2008/04/dataset-xml-serializer-utility-for-unit-testing.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
