<?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: Unit Tests: Boldly Crossing Boundaries and Gently Breaking Rules</title>
	<atom:link href="http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/feed/" rel="self" type="application/rss+xml" />
	<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/</link>
	<description>What would OJ do?</description>
	<lastBuildDate>Fri, 23 Jul 2010 14:33:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: OJ</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1060</link>
		<dc:creator>OJ</dc:creator>
		<pubDate>Wed, 27 Aug 2008 04:54:10 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1060</guid>
		<description>@Jon: Oh cool! It&#039;s nice to hear that you&#039;ve been a subscriber for a while. That makes me happy :)

I fear that we&#039;ll never all agree on a single answer to the question. That&#039;s the pain about issues like this which are very subjective and very much down to taste. Having said that I hope that I&#039;m wrong :)

Thanks for the comments!</description>
		<content:encoded><![CDATA[<p>@Jon: Oh cool! It&#8217;s nice to hear that you&#8217;ve been a subscriber for a while. That makes me happy <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /><br />
I fear that we&#8217;ll never all agree on a single answer to the question. That&#8217;s the pain about issues like this which are very subjective and very much down to taste. Having said that I hope that I&#8217;m wrong <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /><br />
Thanks for the comments!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Limjap</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1059</link>
		<dc:creator>Jon Limjap</dc:creator>
		<pubDate>Tue, 26 Aug 2008 10:52:40 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1059</guid>
		<description>OJ,

Yep, I came from stackoverflow :) although I have had your feed on my GoogleReader for some time already (forgot via which article).

Great blog! I do hope that we find the answer for that question one day.</description>
		<content:encoded><![CDATA[<p>OJ,</p>
<p>Yep, I came from stackoverflow <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> although I have had your feed on my GoogleReader for some time already (forgot via which article).</p>
<p>Great blog! I do hope that we find the answer for that question one day.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OJ</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1058</link>
		<dc:creator>OJ</dc:creator>
		<pubDate>Tue, 26 Aug 2008 10:04:20 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1058</guid>
		<description>@H: Great comment mate. I fixed up your &quot;code&quot; a bit to make it a little more readable. I can see what you&#039;re talking about but again you&#039;re still hitting your DB, which is what the purists think you shouldn&#039;t be doing. Hard one to nail.

@Jon: Welcome to the blog (no doubt you came from &lt;a href=&quot;http://stackoverflow.com/&quot; title=&quot;StackOverflow&quot; rel=&quot;nofollow&quot;&gt;StackOverflow&lt;/a&gt; eh? :) ) Thanks for the comment. There are some nifty features of the unit testing frameworks which do help clean things up, but the question still remains: should we be doing it anyway! :)</description>
		<content:encoded><![CDATA[<p>@H: Great comment mate. I fixed up your &#8220;code&#8221; a bit to make it a little more readable. I can see what you&#8217;re talking about but again you&#8217;re still hitting your DB, which is what the purists think you shouldn&#8217;t be doing. Hard one to nail.</p>
<p>@Jon: Welcome to the blog (no doubt you came from <a href="http://stackoverflow.com/" title="StackOverflow" rel="nofollow">StackOverflow</a> eh? <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> ) Thanks for the comment. There are some nifty features of the unit testing frameworks which do help clean things up, but the question still remains: should we be doing it anyway! <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Limjap</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1057</link>
		<dc:creator>Jon Limjap</dc:creator>
		<pubDate>Tue, 26 Aug 2008 09:19:13 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1057</guid>
		<description>MbUnit already implements a Rollback attribute, so any persistence unit tests can be run without explicit transaction handling in SetUps and TearDowns.

Quite nifty, although it&#039;s gonna mess a lot with incremental/Identity-keyed tables.</description>
		<content:encoded><![CDATA[<p>MbUnit already implements a Rollback attribute, so any persistence unit tests can be run without explicit transaction handling in SetUps and TearDowns.</p>
<p>Quite nifty, although it&#8217;s gonna mess a lot with incremental/Identity-keyed tables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul H (The legs of MEGA-PAUL)</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1056</link>
		<dc:creator>Paul H (The legs of MEGA-PAUL)</dc:creator>
		<pubDate>Thu, 07 Aug 2008 11:35:55 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1056</guid>
		<description>Hey you guys,

You know I am big fans of you both, its nice for us to debate like this. Reminds me of the good ol&#039; days :-)

Rob, I can totally see the merits of adding all the CRUD operations in one test, if you use this kind of tranny method it &lt;em&gt;may&lt;/em&gt; save you having to hand pick the DB clean if a test fails though.

Maybe this bit of pseudo code would explain the solution I proposed a bit better, I don&#039;t think I gave it a good enough of an attempt last time. I don&#039;t think it is touched by the &lt;em&gt;don&#039;t use Setup or Teardown rules&lt;/em&gt;.


&lt;pre lang=&quot;csharp&quot;&gt;using System.Transactions;
using NUnit.Framework;
using System.Data.SqlClient;

public class TestWithTransaction
{
  [TestFixtureSetUp]
  public void TestSetup ()
  {
    Transaction.Current = new Transaction();
  }

  [TestFixtureTearDown]
  public void TearDown()
  {
    Transaction.Current.Rollback();
  }
}

[TestFixture]
public class NormalTest: TestWithTransaction
{
  [Test]
  public void AtoB ()
  {
    CustomerRepository.DoSomething();
  }
}

public class CustomerRepository
{
  public static void DoSomething()
  {
    //So if we do not have a current
    //distributed Tranny we can start one.
    if (Transaction.Current == null)
      Transaction.Current == new Transaction()

    // now we will have a transaction local
    //to here or a distributed one with several
    //command tied up in it.
    SqlConnection connection = new SqlConnection(&quot;&quot;)
    connection.EnlistDistributedTransaction(
    Transaction.Current);
  }
}&lt;/pre&gt;

Never knew you had a blog Rob, send me the url, always interested to hear what you are thinking.

Oj, Yeah totally agree with your point about having stuff like nested trannies and it degrading test performance. If you have this kind of setup above you can dig yourself that hole quite easily.

I think it is pretty useful if you are doing some vanilla Db work and want a nice sandbox for your tests to play in.

One problem with this method is though that if you do not have you data access object setup like this for handling transactions then you will have to change your underlying code to support this which breaks a few rules. And therefore that is where Robs solution of all the CRUD in one test makes perfect sense.</description>
		<content:encoded><![CDATA[<p>Hey you guys,</p>
<p>You know I am big fans of you both, its nice for us to debate like this. Reminds me of the good ol&#8217; days <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':-)' class='wp-smiley' /><br />
Rob, I can totally see the merits of adding all the CRUD operations in one test, if you use this kind of tranny method it <em>may</em> save you having to hand pick the DB clean if a test fails though.</p>
<p>Maybe this bit of pseudo code would explain the solution I proposed a bit better, I don&#8217;t think I gave it a good enough of an attempt last time. I don&#8217;t think it is touched by the <em>don&#8217;t use Setup or Teardown rules</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Transactions</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">NUnit.Framework</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Data.SqlClient</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> TestWithTransaction
<span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#91;</span>TestFixtureSetUp<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TestSetup <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    Transaction.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Transaction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #000000;">&#91;</span>TestFixtureTearDown<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TearDown<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    Transaction.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Rollback</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#91;</span>TestFixture<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NormalTest<span style="color: #008000;">:</span> TestWithTransaction
<span style="color: #000000;">&#123;</span>
  <span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AtoB <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    CustomerRepository.<span style="color: #0000FF;">DoSomething</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerRepository
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> DoSomething<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//So if we do not have a current</span>
    <span style="color: #008080; font-style: italic;">//distributed Tranny we can start one.</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Transaction.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
      Transaction.<span style="color: #0000FF;">Current</span> <span style="color: #008000;">==</span> <span style="color: #008000;">new</span> Transaction<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// now we will have a transaction local</span>
    <span style="color: #008080; font-style: italic;">//to here or a distributed one with several</span>
    <span style="color: #008080; font-style: italic;">//command tied up in it.</span>
    SqlConnection connection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span>
    connection.<span style="color: #0000FF;">EnlistDistributedTransaction</span><span style="color: #000000;">&#40;</span>
    Transaction.<span style="color: #0000FF;">Current</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Never knew you had a blog Rob, send me the url, always interested to hear what you are thinking.</p>
<p>Oj, Yeah totally agree with your point about having stuff like nested trannies and it degrading test performance. If you have this kind of setup above you can dig yourself that hole quite easily.</p>
<p>I think it is pretty useful if you are doing some vanilla Db work and want a nice sandbox for your tests to play in.</p>
<p>One problem with this method is though that if you do not have you data access object setup like this for handling transactions then you will have to change your underlying code to support this which breaks a few rules. And therefore that is where Robs solution of all the CRUD in one test makes perfect sense.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob G</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1055</link>
		<dc:creator>Rob G</dc:creator>
		<pubDate>Thu, 07 Aug 2008 09:10:52 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1055</guid>
		<description>Hey H - nice to see you still care :-)

You&#039;re right about your solution being feasible, and yes it&#039;s entirely feasible with LINQ too, but my personal approach is to keep it as simple as possible, and writing transaction code feels a bit too much like writing code that ONLY satisfies/helps a unit test and has no other real value.

The way I see it, (as in my example in the article) is that if I&#039;m going to write the code to create, and the code to delete - I may as well use them together in the unit test since I&#039;m already breaking some unit testing rules, in which case - I&#039;ve written no extra code that is purely for the unit test.

In addition, I &lt;a href=&quot;http://jamesnewkirk.typepad.com/posts/2007/09/why-you-should-.html&quot; rel=&quot;nofollow&quot;&gt;don&#039;t use Setup or TearDown&lt;/a&gt; anymore because I rather keep the production code DRY, but my tests &lt;a href=&quot;http://www.shaneharvie.com/2007/07/production-code-needs-to-be-dry-tests.html&quot; rel=&quot;nofollow&quot;&gt;MOIST&lt;/a&gt; for ease of maintenance...

I can write some new articles about all those concepts too - including what I do when it&#039;s not a PHYSICAL delete, but rather a LOGICAL delete. Doesn&#039;t seem there&#039;s much interest there though - but we&#039;ll see.

Cheers,
Rob</description>
		<content:encoded><![CDATA[<p>Hey H &#8211; nice to see you still care <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':-)' class='wp-smiley' /><br />
You&#8217;re right about your solution being feasible, and yes it&#8217;s entirely feasible with LINQ too, but my personal approach is to keep it as simple as possible, and writing transaction code feels a bit too much like writing code that ONLY satisfies/helps a unit test and has no other real value.</p>
<p>The way I see it, (as in my example in the article) is that if I&#8217;m going to write the code to create, and the code to delete &#8211; I may as well use them together in the unit test since I&#8217;m already breaking some unit testing rules, in which case &#8211; I&#8217;ve written no extra code that is purely for the unit test.</p>
<p>In addition, I <a href="http://jamesnewkirk.typepad.com/posts/2007/09/why-you-should-.html" rel="nofollow">don&#8217;t use Setup or TearDown</a> anymore because I rather keep the production code DRY, but my tests <a href="http://www.shaneharvie.com/2007/07/production-code-needs-to-be-dry-tests.html" rel="nofollow">MOIST</a> for ease of maintenance&#8230;</p>
<p>I can write some new articles about all those concepts too &#8211; including what I do when it&#8217;s not a PHYSICAL delete, but rather a LOGICAL delete. Doesn&#8217;t seem there&#8217;s much interest there though &#8211; but we&#8217;ll see.</p>
<p>Cheers,<br />
Rob</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OJ</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1053</link>
		<dc:creator>OJ</dc:creator>
		<pubDate>Thu, 07 Aug 2008 00:28:24 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1053</guid>
		<description>Long time no see H :)

I don&#039;t really have too much of a problem with this approach, except for cases where you might end up with nested transactions. It does tend to slow your unit tests down too.

I can&#039;t see why it wouldn&#039;t be compatible with Linq? :)</description>
		<content:encoded><![CDATA[<p>Long time no see H <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /><br />
I don&#8217;t really have too much of a problem with this approach, except for cases where you might end up with nested transactions. It does tend to slow your unit tests down too.</p>
<p>I can&#8217;t see why it wouldn&#8217;t be compatible with Linq? <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul H (The legs of MEGA-PAUL)</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1054</link>
		<dc:creator>Paul H (The legs of MEGA-PAUL)</dc:creator>
		<pubDate>Wed, 06 Aug 2008 12:34:22 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1054</guid>
		<description>Hey Rob,

Nice post. I couldn&#039;t agree more on your points on the posts on here and the other blog. It is sometimes a lot easier to stay in the world of theory rather than down at the coal face, at the &#039;business end&#039; as I call it.

Had this problem myself before and saw a solution implemented by a team I joined for this problem in the System.Transactions namespace.

Basically I think that a TransactionScope was setup on TestFixtureSetup, all the data calls were enlisted into that and then the TransactionScope was rolled back on TestFixtureTearDown, this gave the testing a safe kind of &#039;sandbox&#039; to play in.

So this could give a solution to the whole &#039;what if one fails problem&#039;, as it is in one big trannie.

Whether this is compatible with linq I am not too sure though.

Interested to hear your thoughts on this.</description>
		<content:encoded><![CDATA[<p>Hey Rob,</p>
<p>Nice post. I couldn&#8217;t agree more on your points on the posts on here and the other blog. It is sometimes a lot easier to stay in the world of theory rather than down at the coal face, at the &#8216;business end&#8217; as I call it.</p>
<p>Had this problem myself before and saw a solution implemented by a team I joined for this problem in the System.Transactions namespace.</p>
<p>Basically I think that a TransactionScope was setup on TestFixtureSetup, all the data calls were enlisted into that and then the TransactionScope was rolled back on TestFixtureTearDown, this gave the testing a safe kind of &#8216;sandbox&#8217; to play in.</p>
<p>So this could give a solution to the whole &#8216;what if one fails problem&#8217;, as it is in one big trannie.</p>
<p>Whether this is compatible with linq I am not too sure though.</p>
<p>Interested to hear your thoughts on this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OJ</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1051</link>
		<dc:creator>OJ</dc:creator>
		<pubDate>Tue, 29 Jul 2008 00:51:42 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1051</guid>
		<description>Great comment Sparky. I hope you don&#039;t mind but I modified it to include the code tags so that it was a bit easier to read. :)

One of the issues that I have with allowing unit tests to grow into integration tests is that it then becomes a &quot;standard&quot;. Over time, other devs get wind of this and no longer make sure that their tests actually remain meaningful at the unit level. In short, all the unit tests become integration tests. Yes this is more of a discipline thing than anything else, but it does happen. If you go down this path you also have the issue of multiple tests covering the same functionality, which results in more maintenance of your tests when things change. It&#039;s bad enough that you have to maintain the tests when the code changes, but having to do it across multiple tests is just horrid.

Yup, I&#039;m whinging about relatively minor stuff. But at the end of the day, the cost of maintenance should be kept low and having unit test code attempt to cover more than the unit level increases that cost.

Thanks for the comment :)</description>
		<content:encoded><![CDATA[<p>Great comment Sparky. I hope you don&#8217;t mind but I modified it to include the code tags so that it was a bit easier to read. <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /><br />
One of the issues that I have with allowing unit tests to grow into integration tests is that it then becomes a &#8220;standard&#8221;. Over time, other devs get wind of this and no longer make sure that their tests actually remain meaningful at the unit level. In short, all the unit tests become integration tests. Yes this is more of a discipline thing than anything else, but it does happen. If you go down this path you also have the issue of multiple tests covering the same functionality, which results in more maintenance of your tests when things change. It&#8217;s bad enough that you have to maintain the tests when the code changes, but having to do it across multiple tests is just horrid.</p>
<p>Yup, I&#8217;m whinging about relatively minor stuff. But at the end of the day, the cost of maintenance should be kept low and having unit test code attempt to cover more than the unit level increases that cost.</p>
<p>Thanks for the comment <img src='http://buffered.io/wp-content/plugins/smilies-themer/Silk/emoticon_smile.png' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sparky</title>
		<link>http://buffered.io/2008/07/29/unit-tests-boldly-crossing-boundaries-and-gently-breaking-rules/comment-page-1/#comment-1052</link>
		<dc:creator>sparky</dc:creator>
		<pubDate>Tue, 29 Jul 2008 00:09:52 +0000</pubDate>
		<guid isPermaLink="false">http://buffered.io/?p=424#comment-1052</guid>
		<description>Sometimes you need to check that things come out of persistence layers as you expect them to, or make it through business layers as expected. Mocking and stubs are great tools, but why can&#039;t you also use NUnit for some integration testing?

One thing that I have done for this scenario is to have the test fixture class have a helper class that is a private member of the test fixture. The helper class knows how to delete items of type x, and anything that references those items. The helper class is responsable for cleaning up after the created db rows. Preferably the cleanup code goes directly to the database.

&lt;pre lang=&quot;csharp&quot;&gt;CustomerHelper {
 List&lt;int&gt; ids;
 public void AddToDeleteList(int value) { _ids.Add(value) };
 public void Cleanup() {
 if( !ids.Any() ) return;

 // execute some sql that uses an IN clause. execute all the sql at once.
 string sql = string.Format( &quot;delete from tablex where tablexid in {0}&quot;,
                      ids.ToCsv() );

 try { db.Execute(sql); } catch ...
 ids.Clear();
}&lt;/pre&gt;

The testfixture then holds reference to the helper that it needs, and executes the helper in the test fixture teardown. Don&#039;t put these inside the test method itself, because the test can fail, and we want to ensure the db will be cleaned up if at all possible. By adding the helper to the test fixture level, you can not only have simple cruds, but any number of test methods can add to the delete list, content that the new object will be deleted.

&lt;pre lang=&quot;csharp&quot;&gt;private CustomerHelper _customerHelper;&lt;/pre&gt;

Inside the test method, immediately after the first update() you store the id for later deletion.
&lt;pre lang=&quot;csharp&quot;&gt;_customerHelper.AddToDeleteList(newId);&lt;/pre&gt;

You can definitely make this more robust, but that&#039;s the essence of it all. One of the other reasons this is done is so that certain configurations can be constructed in the db and later cleaned up. No more relying that test code &#039;knows&#039; that ID 1234 is a Manager, and ID 3456 is a graduate employee. Your test creates entities in the database and cleans them up for you.

Also, typically, you assign a specific category to the test (ie DBIntegration) and only run those daily, because they do take a long time to run, but a daily test by Cruise is better than no run at all.

* I&#039;m tempted by IDisposable for the helper class, but can I guarantee that the db is still legitimate when IDisposable is called? Maybe IDisposable as a tracking method, then at least you know if the teardown never happened and can log that somewhere.</description>
		<content:encoded><![CDATA[<p>Sometimes you need to check that things come out of persistence layers as you expect them to, or make it through business layers as expected. Mocking and stubs are great tools, but why can&#8217;t you also use NUnit for some integration testing?</p>
<p>One thing that I have done for this scenario is to have the test fixture class have a helper class that is a private member of the test fixture. The helper class knows how to delete items of type x, and anything that references those items. The helper class is responsable for cleaning up after the created db rows. Preferably the cleanup code goes directly to the database.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">CustomerHelper <span style="color: #000000;">&#123;</span>
 List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> ids<span style="color: #008000;">;</span>
 <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> AddToDeleteList<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> value<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> _ids.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>value<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
 <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Cleanup<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
 <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> <span style="color: #008000;">!</span>ids.<span style="color: #0000FF;">Any</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> return<span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #008080; font-style: italic;">// execute some sql that uses an IN clause. execute all the sql at once.</span>
 <span style="color: #FF0000;">string</span> sql <span style="color: #008000;">=</span> <span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;delete from tablex where tablexid in {0}&quot;</span>,
                      ids.<span style="color: #0000FF;">ToCsv</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span> db.<span style="color: #0000FF;">Execute</span><span style="color: #000000;">&#40;</span>sql<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span> ...
 <span style="color: #0000FF;">ids</span>.<span style="color: #0000FF;">Clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The testfixture then holds reference to the helper that it needs, and executes the helper in the test fixture teardown. Don&#8217;t put these inside the test method itself, because the test can fail, and we want to ensure the db will be cleaned up if at all possible. By adding the helper to the test fixture level, you can not only have simple cruds, but any number of test methods can add to the delete list, content that the new object will be deleted.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> CustomerHelper _customerHelper<span style="color: #008000;">;</span></pre></div></div>

<p>Inside the test method, immediately after the first update() you store the id for later deletion.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">_customerHelper.<span style="color: #0000FF;">AddToDeleteList</span><span style="color: #000000;">&#40;</span>newId<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>You can definitely make this more robust, but that&#8217;s the essence of it all. One of the other reasons this is done is so that certain configurations can be constructed in the db and later cleaned up. No more relying that test code &#8216;knows&#8217; that ID 1234 is a Manager, and ID 3456 is a graduate employee. Your test creates entities in the database and cleans them up for you.</p>
<p>Also, typically, you assign a specific category to the test (ie DBIntegration) and only run those daily, because they do take a long time to run, but a daily test by Cruise is better than no run at all.</p>
<p>* I&#8217;m tempted by IDisposable for the helper class, but can I guarantee that the db is still legitimate when IDisposable is called? Maybe IDisposable as a tracking method, then at least you know if the teardown never happened and can log that somewhere.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
