<?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>Elegant Code &#187; Unit Testing</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Testing a Membership Provider</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Other Assertions in VS Unit</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Elegant Code &#187; Unit Testing</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Testing a Membership Provider</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Other Assertions in VS Unit</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Unit Testing</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Testing a Membership Provider</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Other Assertions in VS Unit</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Unit Testing</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Testing a Membership Provider</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Other Assertions in VS Unit</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Unit Testing</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Testing a Membership Provider</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Other Assertions in VS Unit</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Code &#187; Unit Testing</title>
	<atom:link href="http://elegantcode.com/tag/unit-testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 10:00:00 +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>Testing a Membership Provider</title>
		<link>http://elegantcode.com/2008/04/17/testing-a-membership-provider/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing-a-membership-provider</link>
		<comments>http://elegantcode.com/2008/04/17/testing-a-membership-provider/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 16:21:43 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/17/testing-a-membership-provider/</guid>
		<description><![CDATA[Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &#34;test-only&#34; situations where you can't run against a &#34;real&#34; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership [...]]]></description>
			<content:encoded><![CDATA[<p>Here's something that I was toying with yesterday: creating a MembershipProvider that does all it's storage in-memory.&#160; This would be used in &quot;test-only&quot; situations where you can't run against a &quot;real&quot; provider.&#160; Should be easy, right?&#160; I mean, this provider barely does anything.&#160; Instead, we end up needing various source views, a few different membership classes, and some reflection.</p>  <h4>First Task: MembershipProvider.CreateUser</h4>  <p>There is some logic involved in maintaining the in-memory information, and I certainly don't want to have to run a web application to test the functionality as I'm building it, so the first thing is to create a &quot;.Test&quot; assembly.&#160; </p>  <p>My first test for creating a MembershipUser looked something like this:</p>  <pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> CreateUser()
{
    <span class="rem">// this is what we're testing:</span>
    <span class="kwrd">var</span> provider = <span class="kwrd">new</span> VirtualMembershipProvider();
    
    <span class="rem">// creation of all of these args omitted for brevity..</span>
    <span class="kwrd">var</span> user = provider.CreateUser(...);
    
    <span class="rem">// verify that the MembershipUser is valid..</span>
    Assert.AreEqual(user.UserName, userName); <span class="rem">// etc.etc.etc.</span>
}</pre>

<p>And the implementation of VirtualMembershipProvider.CreateUser():&#160; (obviously I'm leaving out some unimportant details..)</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">override</span> MembershipUser CreateUser(...)
{
    <span class="rem">// MembershipUser takes crazy big number of args..</span>
    MembershipUser user = <span class="kwrd">new</span> MembershipUser(...);
    <span class="rem">// TODO: put that user object somewhere for later access..</span>
    status = MembershipCreateStatus.Success;
    <span class="kwrd">return</span> user;
}</pre>

<p>Can't get much simpler, right?&#160; </p>

<h4>First Issue - Arbitrary Dependency</h4>

<p>Running the test gives an exception from the MembershipUser constructor: &quot;System.ArgumentException : The membership provider name specified is invalid.&quot; So, we jump over to Reflector to see what's going on:</p>

<pre class="csharpcode"><p><span class="kwrd">if</span> ((providerName == <span class="kwrd">null</span>) || <br />    (Membership.Providers[providerName] == <span class="kwrd">null</span>))
{
  <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(...);
}</p></pre>

<p>MembershipUser has a dependency on Membership.Providers.&#160; This is because MembershipUser is not just a data container, but breaks the Single Responsibility Principle (SRP) and has methods like Update() as well.&#160; But, whatever.&#160; Looks like all I've got to do is add my VirtualMembershipProvider into that Membership.Providers collection, and we'll be set.</p>

<h4>Next Issue - Arbitrary ReadOnly-ness</h4>

<p>I put &quot;Membership.Providers.Add()&quot; into the test, to see what happens.&#160; Running the test gives me a new exception: in ProviderCollection.Add() - &quot;System.NotSupportedException : Collection is read-only.&quot;&#160; Argh!&#160; Back to Reflector!&#160; Here's what ProviderCollection.Add() looks like (more or less):</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> Add(ProviderBase provider)
{
    <span class="kwrd">if</span> (<span class="kwrd">this</span>._ReadOnly)
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotSupportedException(...);
    <span class="rem">// some other guard clauses...</span>
    <span class="kwrd">this</span>._Hashtable.Add(provider.Name, provider);
}</pre>

<p>So all I've got to do is call &quot;.IsReadOnly = false&quot; and... ha ha jokes on me.&#160; ProviderCollection has a SetReadOnly() method, but no public way to switch readonly-ness off.&#160; Presumably there's some really good reason for making things difficult with this totally artificial wall that they put in our path.</p>

<p>So, we fall back to the tool of last resort: reflection to get that private _ReadOnly field and set it to false.&#160; Now we can register our provider into the ProviderCollection.&#160; But, what an ugly hack, for seemingly no reason.</p>

<p>One more thing lacking: before the ProviderCollection will accept a Provider, it checks to see if the Provider has been initialized properly.&#160; So we need a call to provider.Initialize() which sets some internal state on the provider.</p>

<p>(Note: by this point, we actually have a fair amount of initialization code here, and its getting complicated.&#160; So this code was moved into its own class &amp; tests - I've left that part out to be more concise.)</p>

<p>More or less, we've got this code to set up our MembershipProvider, outside of the official procedures:</p>

<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Initialize(MembershipProvider provider, 
                              <span class="kwrd">string</span> providerName)
{
    provider.Initialize(providerName, <span class="kwrd">null</span>);
    <span class="rem">// grabs a cached FieldInfo instance and sets it appropriately.</span>
    <span class="rem">// note this would affect all Membershp providers...</span>
    AllowNewProviders = <span class="kwrd">true</span>;
    Membership.Providers.Add(provider);
}</pre>

<p>And now, our CreateUser() test passes and we can get back to work on the REAL problem.&#160; </p>

<h4>Conclusions</h4>

<ul>
  <li>Don't put arbitrary roadblocks in your framework.&#160; You have no idea how somebody will need to use (and abuse) it. </li>

  <li>Strive to keep your code decoupled.&#160; Always a good idea no matter what you're working on. </li>

  <li>This is &quot;running with scissors:&quot; I wouldn't propose subverting the Provider model with this sort of hackery for code that you wanted to use in production, at least not with much more testing and consideration.&#160; There might be (and probably is) a really good reason for that _ReadOnly flag, and we just broke it.. </li>

  <li>And to bring us back to the first point: framework developers should <em>provide the &quot;default safe&quot; implementation </em>with that _ReadOnly flag, but then at least <em>provide a way to cleanly get around the safety mechanism</em>, for those of us who are willing to accept the risk. </li>
</ul>

<p>So, ASP.NET, thanks for the offer of holding my hand through every step of the way, but instead how about you just get out of my way and let me get my work done, kthxbai.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/17/testing-a-membership-provider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.

After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.

The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.

Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<strong>Remove Duplications with Delegates</strong>

Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
Then, in our tests, we replace the following test method...

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
With the same method using our delegate approach.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with "action.Method.Name," or some other intelligent logic to determine what to log.

This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.

<strong>The Aspect-Oriented Approach</strong>

<a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.

If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.

<strong>AOP with Castle DynamicProxy</strong>

<a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle's DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.

The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.

<strong>AOP with PostSharp</strong>

<a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur's PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael's <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.

The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
At this point, all we need to do is apply our aspect on target methods. The "AttributeTargetMembers" attribute can use wildcards. This tells the AOP framework to only intercept those methods in the "MathService" class that start with "Test." Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with "Test" in the method name. This is so that PostSharp can filter what methods to intercept.

<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.

The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.

Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial Mocks explained</title>
		<link>http://elegantcode.com/2008/04/08/partial-mocks-explained/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=partial-mocks-explained</link>
		<comments>http://elegantcode.com/2008/04/08/partial-mocks-explained/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 03:14:42 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/partial-mocks-explained/</guid>
		<description><![CDATA[If you are wondering what is prompting this post, it is the fact that on my last post I got schooled by Jeff Brown of Bits-in-Motion.&#160;&#160; Jeff is also one of the minds behind Gallio and MBUnit.&#160; You can only hope to be told you are completely wrong by such a guy. He was very [...]]]></description>
			<content:encoded><![CDATA[<p>If you are wondering what is prompting this post, it is the fact that on my <a href="http://elegantcode.com/2008/04/07/mocking-nested-methods/">last post</a> I got schooled by Jeff Brown of <a href="http://blog.bits-in-motion.com/">Bits-in-Motion</a>.&nbsp;&nbsp; Jeff is also one of the minds behind <a href="http://www.gallio.org">Gallio</a> and <a href="http://www.mbunit.com/">MBUnit</a>.&nbsp; You can only hope to be told you are completely wrong by such a guy. He was very nice about it actually, just stating a preference for a different way of handling the same problem.&nbsp; My problem was that his solution was better than mine.&nbsp; Much better.</p> <p>Where it led me was to re-investigate <a href="http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx">Partial Mocks in Rhino Mocks</a>.&nbsp; My original misunderstanding of Partial Mocks was that they would only mock Abstract Classes.&nbsp; A partial mock will mock ANY class -- but only the Partial mock will only mock abstract methods.&nbsp; That small misunderstanding has caused me to loose untold amount of hair and good will towards all men.</p> <p>So I'm going to re-implement my class methods.&nbsp; Here you go:</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyClass
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
    {
        <span class="rem">// stuff happens here</span>
        <span class="kwrd">int</span> i = Bar();
        <span class="rem">// do more stuff here    </span>
        <span class="kwrd">return</span> i + 5;
    }
    <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">int</span> Bar()
    {
       <span class="rem">// do some logic</span>
       <span class="kwrd">return</span> 1;
    }
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
A few changes here.&nbsp; First, I removed the delegate and renamed BarInternal back to <strong>Bar</strong>.&nbsp; Second, I marked the <strong>Bar</strong> method as <a href="http://msdn2.microsoft.com/en-us/library/9fkccyh4(VS.80).aspx">virtual</a>.&nbsp; </p>
<p>They key point about a virtual method is that you can provide an implementation, and then a inherited class can completely override that method.&nbsp; That is what the Partial Mock is allowing us to do.</p>
<p>So how do I write my tests?&nbsp; If you don't like using Rhino Mocks you do this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    <span class="kwrd">int</span> expected = 7;
    <span class="kwrd">int</span> result = 0;
    MockRepository mock = <span class="kwrd">new</span> MockRepository();
    MyClass c = mock.PartialMock&lt;MyClass&gt;();

    <span class="kwrd">using</span> (mock.Record())
    {
        Expect.Call(c.Bar()).Return(2);
    }
    <span class="kwrd">using</span> (mock.Playback())
    {
        result = c.Foo();
    }

    Assert.AreEqual(expected, result);
}</pre>
<p>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>
Obviously, now my test is referencing the Rhino Mocks library.&nbsp; The variable mock is the Mock Repository.&nbsp; Now the big change is that I don't create MyClass directly, but through the Rhino Mocks PartialMock method.</p>
<p>Next, I tell the mock repository to expect a call to <strong>c.Bar</strong>, and when called return <strong>2</strong>.&nbsp; That is what is happening in the using (<strong>mock.Record()</strong>) section of the code.&nbsp; Then, in the <strong>mock.Playback</strong> section, I call <strong>Foo().</strong></p>
<p>One other quick note, if we were to rewrite <strong>Foo</strong> such that it no longer called <strong>Bar</strong> this test will fail.&nbsp; The <strong>Expect.Call</strong> part tell the mock repository that the method <strong>Bar</strong> will be called, and if it isn't there is a problem.&nbsp; There are ways around that as well, but that is a topic for another day.</p>
<p>Where does that leave us now?&nbsp; Aside from the odd "<strong>virtual</strong>" keyword thrown into our production code, this is the code as you would have originally written it, even without TDD.&nbsp; I don't like making API changes just to satisfy testing, but I am very OK with this change.</p>
<p>&nbsp;</p>
<p>But what about that new kid on the block?&nbsp; You know, <a href="http://code.google.com/p/moq/">Moq</a>.</p>
<p>I still love Rhino Mocks, but I can't help but want to play with a new toy.&nbsp; So here is the same thing, but in Moq:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_moq_test()
{
    Mock&lt;MyClass&gt; mock = <span class="kwrd">new</span> Mock&lt;MyClass&gt;();
    mock.Expect(x =&gt; x.Bar()).Returns(2);

    MyClass c = mock.Object;
    <span class="kwrd">int</span> result = c.Foo();
    Assert.AreEqual(7, result);
}</pre>
<style type="text/css">.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style>

<p>So as you should be able to see, Moq is a very different animal than RhinoMocks.&nbsp; There is no Record or Playback (the web site calls those confusing, I disagree, but still), and the <strong>Expect</strong> happens with a anonymous delegate (in the form of the Lambda Expression "<strong>x =&gt; x.Bar()</strong>" where x is of type <strong>MyClass</strong>).</p>
<p>The cool part is that you can get the same results with less code using Moq.&nbsp; That has been the main advantage all along.&nbsp; My informal testing also reveals that Moq is just a hair faster than Rhino Mocks (but not fast enough to warrant switching frameworks).</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/partial-mocks-explained/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Mocking nested methods</title>
		<link>http://elegantcode.com/2008/04/07/mocking-nested-methods/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mocking-nested-methods</link>
		<comments>http://elegantcode.com/2008/04/07/mocking-nested-methods/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:16:09 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Mock Objects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/07/mocking-nested-methods/</guid>
		<description><![CDATA[I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&#160; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&#160; Which probably means it is a bad idea.&#160; After all, if the Internet, [...]]]></description>
			<content:encoded><![CDATA[  <p>I am writing this as a way to document something that is possible, but you probably shouldn't do unless you really need to.&nbsp; Also, I'm sure this is not new, I just can't find any mention of it on Google right now.&nbsp; Which probably means it is a bad idea.&nbsp; After all, if the Internet, the keeper of all that is right and true, doesn't know about something it must be wrong.</p> <h2>The problem</h2> <p>You have two methods (Foo and Bar), and Foo calls method Bar.&nbsp; It will look like this:</p> <style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
</style> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here<br></span>&nbsp;&nbsp;&nbsp; return i + 5<br>}

<span class="kwrd">public</span> <span class="kwrd">int</span> Bar()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>Now you need to test both methods.&nbsp; Bar can be easily tested like this (this should work for NUnit and MBUnit tests -- assuming both methods exist in a class call MyClass):</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Bar_Test()
{
    MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Bar();
   Assert.AreEqual(1, result);
}</pre>
<p>Now we test Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> Foo_Test()
{
   MyClass c = <span class="kwrd">new</span> MyClass();
   <span class="kwrd">int</span> result = c.Foo();
   Assert.AreEqual(6, result);
}</pre>
<p>Here is where we have an issue.&nbsp; What if the result of Bar changes?&nbsp; That will break the test.&nbsp; So now we have a brittle test.&nbsp; Any time Bar changes, we have to change all of the Bar tests, but also the Foo tests, so we have added complexity to our tests as well.&nbsp; Now if Foo (or Bar) was in a separate class, this wouldn't be an issue, but they are in the same class.&nbsp; And as much as I like small classes, this seems too trivial to split up (even in the context of an extremely trivial example). </p>
<h2>The Solution</h2>
<p>What I would like to do is Mock the method Bar.&nbsp; You can do that with a delegate.</p>
<p>Without really getting into what a delegate is, I will just show the code.&nbsp; So here are my two methods, with a delegate added in for good measure.</p><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">delegate</span> <span class="kwrd">int</span> BarDelegate();

<span class="kwrd">public</span> BarDelegate Bar;

<span class="kwrd">public</span> MyClass()
{
    Bar = BarInternal;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> Foo()
{
    <span class="rem">// stuff happens here</span>
    <span class="kwrd">int</span> i = Bar();
    <span class="rem">// do more stuff here    </span>
    <span class="kwrd">return</span> i + 5;
}

<span class="kwrd">public</span> <span class="kwrd">int</span> BarInternal()
{
   <span class="rem">// do some logic</span>
   <span class="kwrd">return</span> 1;
}</pre>
<p>OK, here is what I did:</p>
<ol>
<li>Renamed Bar to BarInternal 
<li>BarDelegate delegate 
<li>Created a variable named Bar to point to the method BarInternal 
<li>Created a constructor to hook up Bar to BarInternal.</li></ol>
<p>Now all of my code can still use the method Bar just like it was there, but in actuality it is calling BarInternal.&nbsp; I have now created one layer of indirection to my method Bar so it can now be mocked and tested.</p>
<p>Here is my new test for Foo:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = BarMethodForTest;
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}

<span class="kwrd">private</span> <span class="kwrd">int</span> BarMethodForTest()
{
    <span class="kwrd">return</span> 2;
}</pre>
<p>So what is new with the test is the BarMethodForTest that is passed to c.Bar and I have coded BarMethodForTest to always return 2.&nbsp; When I set c.Bar to my new method, BarInternal (the default method) is no longer called.</p>
<p>Actually, if I wanted to I could simplify this further by setting c.Bar with an anonymous delegate like this:</p><pre class="csharpcode">[Test]
<span class="kwrd">public</span> <span class="kwrd">void</span> FooTest()
{
    MyClass c = <span class="kwrd">new</span> MyClass()
    c.Bar = <span class="kwrd">delegate</span>() { <span class="kwrd">return</span> 2; }
    <span class="kwrd">int</span> result = target.Foo();
    Assert.AreEqual(7, result);
}</pre>
<h2>Stepping Back</h2>
<p>OK, take a step back and look at this again with a critical eye.&nbsp; Have I really solved the problem?&nbsp;&nbsp; Yes and no.&nbsp; The real problem was that my methods were too complex to simply test.&nbsp; Now my methods are simple to test, but the code is complex to read.&nbsp; All I've done is shifted the complexity from one class to the other.&nbsp; A better solution would to have created another class and further abstracted out the logic.</p>
<p>At the same time, my class is not overly burdened by the change either.&nbsp; In lieu of a real refactoring, I do consider this a viable solution to the problem.&nbsp; So by all means do this -- but make sure you are out of alternatives first.</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/07/mocking-nested-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some Other Assertions in VS Unit</title>
		<link>http://elegantcode.com/2008/02/22/some-other-assertions-in-vs-unit/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=some-other-assertions-in-vs-unit</link>
		<comments>http://elegantcode.com/2008/02/22/some-other-assertions-in-vs-unit/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 00:28:12 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/02/22/some-other-assertions-in-vs-unit/</guid>
		<description><![CDATA[Folks are often unaware of a some classes within the Visual Studio unit testing framework that make writing unit tests a bit easier. Instead of simply adding functionality to the base Assert class, the framework declares 2 other specialized assertion classes: CollectionAssert and StringAssert. CollectionAssert is handy for comparing 2 collections or for validating members [...]]]></description>
			<content:encoded><![CDATA[<p>Folks are often unaware of a some classes within the Visual Studio unit testing framework that make writing unit tests a bit easier. Instead of simply adding functionality to the base Assert class, the framework declares 2 other specialized assertion classes: CollectionAssert and StringAssert.</p>  <p>CollectionAssert is handy for comparing 2 collections or for validating members within a single collection. Here is a shot from VS2008 on the CollectionAssert class. This functionality has been there since vs2005, but unless you know it's there you may have not seen it.</p>  <p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="237" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/02/image.png" width="341" border="0" /></p>  <p> String.Assert is a smart little specialty assertion class that helps us work with strings. There functionality in there to let us work with formats and regular expressions.</p>  <p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="237" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/02/image1.png" width="315" border="0" /> </p>  <p>It's true that you can get this functionality from the standard Assert class, but it takes a little more code to pull it off and these things are there to save us the keystrokes. Enjoy!</p>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/02/22/some-other-assertions-in-vs-unit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

