<?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>Damian&#039;s Blog &#187; unit testing</title>
	<atom:link href="http://damianblog.com/category/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://damianblog.com</link>
	<description>.NET from Geneva, Switzerland</description>
	<lastBuildDate>Fri, 30 Sep 2011 23:25:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Moq ExpectSet</title>
		<link>http://damianblog.com/2008/09/17/using-moq-expectset/</link>
		<comments>http://damianblog.com/2008/09/17/using-moq-expectset/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 09:22:09 +0000</pubDate>
		<dc:creator>dmehers</dc:creator>
				<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://damianblog.com/2008/09/17/using-moq-expectset/</guid>
		<description><![CDATA[I am trying out Moq, having used RhinoMocks a fair bit in the past. I was having trouble using the ExpectSet method which verifies that a property has been set, and a google search found nothing that directly answered my question.&#160; I wanted to know how to verify that the value set is what was [...]]]></description>
			<content:encoded><![CDATA[<p>I am trying out <a href="http://code.google.com/p/moq/">Moq</a>, having used <a href="http://www.ayende.com/projects/rhino-mocks.aspx">RhinoMocks</a> a fair bit in the past.</p>
<p>I was having trouble using the <a href="http://www.clariusconsulting.net/labs/moq/html/FE79571.htm">ExpectSet</a> method which verifies that a property has been set, and a google search found nothing that directly answered my question.&#160; I wanted to know how to verify that the value set is what was expected.</p>
<p>It turns out that you need to define a callback in which you have the assertion to verify that the property has been set to the correct value.</p>
<div>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">[TestMethod]
<span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestAppleShiner() {

    <span style="color: #008000">// Mock the interface being passed to the class to be tested</span>
    <span style="color: #0000ff">var</span> fruit = <span style="color: #0000ff">new</span> Mock&lt;IFruit&gt;();

    <span style="color: #008000">// Define the expectation that the Colour property will be</span>
    <span style="color: #008000">// set to Green</span>
    fruit.ExpectSet(f=&gt;f.Colour).Callback(
        setColor=&gt;Assert.AreEqual(<span style="color: #006080">&quot;Green&quot;</span>, setColor)).Verifiable();

    <span style="color: #008000">// Run the test</span>
    ApplePolisher applePolisher = <span style="color: #0000ff">new</span> ApplePolisher();
    applePolisher.Polish(fruit.Object);

    <span style="color: #008000">// Verify that the test passed (note .Verifiable on the ExpectSet)</span>
    fruit.VerifyAll();
}

<span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IFruit {
    <span style="color: #0000ff">string</span> Colour { get; set; }
}

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ApplePolisher {
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Polish(IFruit fruit) {
        fruit.Colour = <span style="color: #006080">&quot;Green&quot;</span>;
    }
}</pre>
</div>
<div>&#160;</div>
<div>After ExpectSet, call the Callback method giving the name of the variable to hold the passed in value, and then the assertion with regards to its value.</div>
]]></content:encoded>
			<wfw:commentRss>http://damianblog.com/2008/09/17/using-moq-expectset/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Handling background threads using NUnit and Rhino Mocks</title>
		<link>http://damianblog.com/2008/03/06/handling-background-threads-using-nunit-and-rhino-mocks/</link>
		<comments>http://damianblog.com/2008/03/06/handling-background-threads-using-nunit-and-rhino-mocks/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 20:46:35 +0000</pubDate>
		<dc:creator>dmehers</dc:creator>
				<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://damianblog.wordpress.com/2008/03/06/handling-background-threads-using-nunit-and-rhino-mocks/</guid>
		<description><![CDATA[I've just started using NUnit and Rhino Mocks, so this is not necessarily the best way to do things, but it works for me. I have a class which has a method which kicks off a background thread.&#160; This thread has an infinite loop -- for each loop it accepts a request and services the [...]]]></description>
			<content:encoded><![CDATA[<p>I've just started using NUnit and Rhino Mocks, so this is not necessarily the best way to do things, but it works for me.</p>
<p>I have a class which has a method which kicks off a background thread.&nbsp; This thread has an infinite loop -- for each loop it accepts a request and services the request.</p>
<p>I needed to test an instance of the class, for one specific incoming request.&nbsp; There are two issues -- how to know when the object's background thread has finished servicing the request, and how to stop the background thread from looping back and accepting another request.</p>
<p>This is what I ended up doing:</p>
<pre>[<span style="color:#2b91af;">TestFixture</span>]
<span style="color:blue;">public class </span><span style="color:#2b91af;">ServerTest </span>{

  [<span style="color:#2b91af;">Test</span>]
  <span style="color:blue;">public void </span>SomeTest() {

    <span style="color:blue;">bool </span>done = <span style="color:blue;">false</span>;

    <span style="color:#2b91af;">MockRepository </span>mocks = <span style="color:blue;">new </span><span style="color:#2b91af;">MockRepository</span>();

    <span style="color:blue;">using</span>(mocks.Record()) {

      <span style="color:green;">// ... lots of 'Expects'

      // This is the final call which gets made in the background thread
      // to complete the handling of the request
      </span><span style="color:#2b91af;">Expect</span>.Call(someObject.LastMethod).Do((<span style="color:#2b91af;">Expect</span>.<span style="color:#2b91af;">Action</span>)<span style="color:blue;">delegate</span>() {
        done = <span style="color:blue;">true</span>;
        System.Threading.<span style="color:#2b91af;">Thread</span>.CurrentThread.Abort();
      });
    }
    <span style="color:blue;">using</span>(mocks.Playback()) {
      <span style="color:#2b91af;">Server </span>server = <span style="color:blue;">new </span><span style="color:#2b91af;">Server</span>();
      server.Start();
      <span style="color:blue;">int </span>count = 0;
      <span style="color:blue;">while</span>(!done &amp;&amp; count &lt; 10) {
        System.Threading.<span style="color:#2b91af;">Thread</span>.Sleep(1000);
        count++;
      }
    }
  }
}
</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The key point is that in the test's action associated with the last method that gets invoked as part of your background thread's processing loop, you need to set a flag so that the test knows that the last method has executed, and also abort the current thread so that the background thread doesn't loop back, and consume another request.</p>
]]></content:encoded>
			<wfw:commentRss>http://damianblog.com/2008/03/06/handling-background-threads-using-nunit-and-rhino-mocks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

