<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Peter Provost's Geek Noise]]></title>
  <link href="http://www.peterprovost.org//atom.xml" rel="self"/>
  <link href="http://www.peterprovost.org//"/>
  <updated>2013-11-07T04:55:55+00:00</updated>
  <id>http://www.peterprovost.org//</id>
  <author>
    <name><![CDATA[Peter Provost]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Playing around with JavaScript modules]]></title>
    <link href="http://www.peterprovost.org//blog/2013/11/06/playing-around-with-javascript-modules/"/>
    <updated>2013-11-06T17:42:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2013/11/06/playing-around-with-javascript-modules</id>
    <content type="html"><![CDATA[<p>Recently I&rsquo;ve been playing around a lot with JavaScript modules.  The
particular use case I&rsquo;ve been thinking about is the creation of a large complex
JavaScript library in a modular and sensible way. JavaScript doesn&rsquo;t really do
this very well. It does it so poorly, in fact, that a sizeable number of
projects are all done in a single file. I did fine a number that used file
concatenation to assemble the output scrpit, but this seems like a stone-age
technique to me.</p>

<p>This led me to look at the two competing JavaScript module techniques:
<a href="https://github.com/amdjs/amdjs-api/wiki/AMD">Asynchronous Module Definition</a> (AMD) and <a href="http://wiki.commonjs.org/wiki/Modules/1.1">CommonJS</a> (CJS). AMD is the
technique used in <a href="http://www.requirejs.org/">RequireJS</a>) and CommonJS is the technique used by
<a href="http://nodejs.org/">nodejs</a>.</p>

<!-- more -->

<p>The RequireJS project has a script called r.js which will &ldquo;compile&rdquo; a set of
AMD  modules into a single file. There are other projects like <a href="http://browserify.org/">Browserify</a>
which do the same thing for a collection of CommonJS modules  Basically, all of
these figure out the ordering from the dependencies, concatenate the files, and
inject a minimalistic bootstrapper to provide the require/module/exports
functions. Unfortunately, this means that they all have the downside of leaving
all the &lsquo;cruft&rsquo; of the module specification in the resulting file.</p>

<p>To illustrate what I mean by &lsquo;cruft&rsquo;, I will use 
<a href="https://github.com/substack/node-browserify/tree/master/example/api/browser">one of the examples from the browserify project</a>.  This project has three
JavaScript files that use the CommonJS module syntax, and depend on each other
in a chain.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>bar.js </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
</pre></td><td class="code"><pre><code class="javascript"><span class="line"><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">	<span class="k">return</span> <span class="nx">n</span> <span class="o">*</span> <span class="mi">3</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>foo.js </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="javascript"><span class="line"><span class="kd">var</span> <span class="nx">bar</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./bar&#39;</span><span class="p">);</span>
</span><span class="line"><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">	<span class="k">return</span> <span class="nx">n</span> <span class="o">*</span> <span class="nx">bar</span><span class="p">(</span><span class="nx">n</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>main.js </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
</pre></td><td class="code"><pre><code class="javascript"><span class="line"><span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./foo&#39;</span><span class="p">);</span>
</span><span class="line"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">foo</span><span class="p">(</span><span class="mi">5</span><span class="p">));</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>When I run this through browserify, it produces this output:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>out.js </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
</pre></td><td class="code"><pre><code class="javascript"><span class="line"><span class="p">;(</span><span class="kd">function</span> <span class="nx">e</span><span class="p">(</span><span class="nx">t</span><span class="p">,</span><span class="nx">n</span><span class="p">,</span><span class="nx">r</span><span class="p">){</span><span class="kd">function</span> <span class="nx">s</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">u</span><span class="p">){</span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">n</span><span class="p">[</span><span class="nx">o</span><span class="p">]){</span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">t</span><span class="p">[</span><span class="nx">o</span><span class="p">]){</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">a</span><span class="o">=</span><span class="k">typeof</span> <span class="nx">require</span><span class="o">==</span><span class="s2">&quot;function&quot;</span><span class="o">&amp;&amp;</span><span class="nx">require</span><span class="p">;</span><span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="nx">u</span><span class="o">&amp;&amp;</span><span class="nx">a</span><span class="p">)</span><span class="k">return</span> <span class="nx">a</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="o">!</span><span class="mi">0</span><span class="p">);</span>
</span><span class="line"><span class="k">if</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span><span class="k">return</span> <span class="nx">i</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="o">!</span><span class="mi">0</span><span class="p">);</span><span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">&quot;Cannot find module &#39;&quot;</span><span class="o">+</span><span class="nx">o</span><span class="o">+</span><span class="s2">&quot;&#39;&quot;</span><span class="p">)</span>
</span><span class="line"><span class="p">}</span><span class="kd">var</span> <span class="nx">f</span><span class="o">=</span><span class="nx">n</span><span class="p">[</span><span class="nx">o</span><span class="p">]</span><span class="o">=</span><span class="p">{</span><span class="nx">exports</span><span class="o">:</span><span class="p">{}};</span><span class="nx">t</span><span class="p">[</span><span class="nx">o</span><span class="p">][</span><span class="mi">0</span><span class="p">].</span><span class="nx">call</span><span class="p">(</span><span class="nx">f</span><span class="p">.</span><span class="nx">exports</span><span class="p">,</span><span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">){</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">n</span><span class="o">=</span><span class="nx">t</span><span class="p">[</span><span class="nx">o</span><span class="p">][</span><span class="mi">1</span><span class="p">][</span><span class="nx">e</span><span class="p">];</span><span class="k">return</span> <span class="nx">s</span><span class="p">(</span><span class="nx">n</span><span class="o">?</span><span class="nx">n</span><span class="o">:</span><span class="nx">e</span><span class="p">)},</span><span class="nx">f</span><span class="p">,</span><span class="nx">f</span><span class="p">.</span><span class="nx">exports</span><span class="p">,</span><span class="nx">e</span><span class="p">,</span><span class="nx">t</span><span class="p">,</span><span class="nx">n</span><span class="p">,</span><span class="nx">r</span><span class="p">)}</span><span class="k">return</span> <span class="nx">n</span><span class="p">[</span><span class="nx">o</span><span class="p">].</span><span class="nx">exports</span>
</span><span class="line"><span class="p">}</span><span class="kd">var</span> <span class="nx">i</span><span class="o">=</span><span class="k">typeof</span> <span class="nx">require</span><span class="o">==</span><span class="s2">&quot;function&quot;</span><span class="o">&amp;&amp;</span><span class="nx">require</span><span class="p">;</span><span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">o</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="nx">o</span><span class="o">&lt;</span><span class="nx">r</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="nx">o</span><span class="o">++</span><span class="p">)</span><span class="nx">s</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="nx">o</span><span class="p">])</span>
</span><span class="line"><span class="p">;</span><span class="k">return</span> <span class="nx">s</span><span class="p">})({</span><span class="mi">1</span><span class="o">:</span><span class="p">[</span><span class="kd">function</span><span class="p">(</span><span class="nx">require</span><span class="p">,</span><span class="nx">module</span><span class="p">,</span><span class="nx">exports</span><span class="p">){</span>
</span><span class="line"><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">	<span class="k">return</span> <span class="nx">n</span> <span class="o">*</span> <span class="mi">3</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="p">},{}],</span><span class="mi">2</span><span class="o">:</span><span class="p">[</span><span class="kd">function</span><span class="p">(</span><span class="nx">require</span><span class="p">,</span><span class="nx">module</span><span class="p">,</span><span class="nx">exports</span><span class="p">){</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">bar</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./bar&#39;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line"><span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">	<span class="k">return</span> <span class="nx">n</span> <span class="o">*</span> <span class="nx">bar</span><span class="p">(</span><span class="nx">n</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="p">},{</span><span class="s2">&quot;./bar&quot;</span><span class="o">:</span><span class="mi">1</span><span class="p">}],</span><span class="mi">3</span><span class="o">:</span><span class="p">[</span><span class="kd">function</span><span class="p">(</span><span class="nx">require</span><span class="p">,</span><span class="nx">module</span><span class="p">,</span><span class="nx">exports</span><span class="p">){</span>
</span><span class="line"><span class="kd">var</span> <span class="nx">foo</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./foo&#39;</span><span class="p">);</span>
</span><span class="line"><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">foo</span><span class="p">(</span><span class="mi">5</span><span class="p">));</span>
</span><span class="line">
</span><span class="line"><span class="p">},{</span><span class="s2">&quot;./foo&quot;</span><span class="o">:</span><span class="mi">2</span><span class="p">}]},{},[</span><span class="mi">3</span><span class="p">])</span>
</span><span class="line"><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>When you run the resulting program in either a browser or nodejs, it does the
right thing and tells you 75. But look at how much garbage there is! To deal
with the require &amp; module parts, it defines a bunch of stuff at the top,
seriously bloating the file. The original program code was 9 lines of code with
169 characters). The &ldquo;compiled&rdquo; version was 14 lines and 733 characters.
Minifying it with <a href="https://github.com/mishoo/UglifyJS">uglifyjs</a> only shrinks it down to 713 characters.</p>

<p>In an ideal world, I would like system that can produce this from those same
three source files:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>ideal.js </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="javascript"><span class="line"><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class="line">	<span class="kd">function</span> <span class="nx">bar</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">		<span class="k">return</span> <span class="nx">n</span> <span class="o">*</span> <span class="mi">3</span><span class="p">;</span>
</span><span class="line">	<span class="p">}</span>
</span><span class="line">	<span class="kd">function</span> <span class="nx">foo</span><span class="p">(</span><span class="nx">n</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">		<span class="k">return</span> <span class="nx">n</span> <span class="o">*</span> <span class="nx">bar</span><span class="p">(</span><span class="nx">n</span><span class="p">);</span>
</span><span class="line">	<span class="p">}</span>
</span><span class="line">	<span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">foo</span><span class="p">(</span><span class="mi">5</span><span class="p">));</span>
</span><span class="line"><span class="p">}());</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>That looks better to me and is only 114 characters long (minified it is 95).
Because I used the module pattern, like the compiled version, it doesn&rsquo;t leak
anything into the global namespace.</p>

<p>I&rsquo;d read that <a href="http://jquery.com/">JQuery</a> used AMD modules, and knew that their resulting JavaScript
files weren&rsquo;t cluttered up, so I took a look at their <a href="https://github.com/jquery/jquery/blob/master/build/tasks/build.js">build script</a>.
Interestingly they use the optimizer from RequireJS, but then do a bunch of
post processing.  Essentially they rip out all the declares and requires and
such, and then wrap it in a closure. Pretty cool.</p>

<p>I found a number of other tools for doing single-file compiles, but none of
them seemed to take the kind of aggressive approach to optimization that I
think would be ideal when building a large library, so for now I&rsquo;ve been using
a hacked up version of the JQuery build system.</p>

<p>I like modular code, but I&rsquo;m still not very happy about what I&rsquo;ve found for
JavaScript. I can get cohesive, decoupled and testable components using either
the AMD or CommonJS approach, but still haven&rsquo;t found a reasonable way to bring
it all together when building a single library.</p>

<p>Next up, I&rsquo;m going to look at what I can get from <a href="http://www.typescriptlang.org/">TypeScript</a> modules. Maybe it will give
me more of what I want.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What Really Matters in Developer Testing]]></title>
    <link href="http://www.peterprovost.org//blog/2013/09/23/what-really-matters-in-developer-testing/"/>
    <updated>2013-09-23T10:07:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2013/09/23/what-really-matters-in-developer-testing</id>
    <content type="html"><![CDATA[<p>Last month I wrote an article for Visual Studio Magazine called, &ldquo;What Really
Matters in Developer Testing&rdquo; that I wanted to share with my readers here.</p>

<p>Note: They changed the title and made a few other tweaks here and there, so this
is my original manuscript as opposed to the edited and published version. Enjoy!</p>

<p>If you&rsquo;d prefer, you can read the published article here:<br />
<a href="http://visualstudiomagazine.com/articles/2013/08/01/what-really-matters-in-developer-testing.aspx?m=1">http://visualstudiomagazine.com/articles/2013/08/01/what-really-matters-in-developer-testing.aspx?m=1</a></p>

<!-- More -->

<h2 id="what-really-matters-in-developer-testing">What Really Matters In Developer Testing</h2>

<p><strong>It isn’t about the tests, it is about the feedback</strong></p>

<p><em>by Peter Provost – Principal Program Manager Lead – Microsoft Visual Studio</em></p>

<h3 id="introduction">Introduction</h3>

<p>Recently someone asked me to provide guidance to someone wanting “convince
development teams to adopt TDD”. I wrote a rather long reply that was the seed
leading to this article. My answer was simple: you cannot convince people to do
Test-Driven Development (TDD). In fact, you should not even try because it
focuses on the wrong thing.</p>

<p>TDD is a technique, not an objective. What really matters to developers that
their code does what they expect it to do. They want to be able to quickly
implement their code, and know that it does what they want. By focusing on
this, we can more easily see how developers can use testing techniques to
achieve their goals.</p>

<p>All developers do some kind of testing when they write code. Even those who
claim not to write tests actually do. Many will simply create little command
line programs to validate expected outcomes. Others will create special modes
for their app that allow them to confirm different behaviors. Nevertheless, all
developers do some kind of testing while working on their code.</p>

<h3 id="what-test-driven-development-is-and-isnt">What Test-Driven Development Is (and Isn’t)</h3>

<p>Unlike ad-hoc methods like those described above, TDD defines a regimented
approach to writing code. The essence of it is to define and write a small test
for what you want before you write the code that implements it. Then you make
the test pass, refactor as appropriate and repeat. One can view TDD as a kind
of scientific method for code, where you create a hypothesis (what you expect),
define your experiment (the tests) and then run the experiment on your subject
(the code).</p>

<p>TDD proponents assign additional benefits to this technique. One of the most
important is that it strongly encourages a highly cohesive, loosely coupled
design. Since the test defines the interface and how dependencies are provided,
the resulting code typically ends up easy to isolate and with a single purpose.
Object-oriented design defines high cohesion and loose coupling to be essential
properties of well-designed components.</p>

<p>In addition, TDD provides a scaffold for when the developer is unsure how to
implement something. It allows the developer to have a conversation with the
code, asking it questions, getting answers and the adjusting. This makes it a
great exploratory tool for the understanding something new.</p>

<p>Test-driven development is not a panacea, however. While the tests produced can
serve to protect from regressions, they are not sufficient on their own to
assess and ensure quality. Integration tests that combine several units
together are essential to establish a complete picture of quality. End-to-end
and exploratory testing will still be required to evaluate the fitness of the
entire system.</p>

<p>In short, TDD is another tool that the developer can use while writing code. It
has benefits but it also has costs. It can help you define components with very
little upfront definition. However, it will add to the time required to create
the code. It has a strong, positive impact on the design of your code, but it
requires practice and experience to learn, and can be frustrating for the
novice.</p>

<h3 id="short-cycle-mode">Short-cycle Mode</h3>

<p>When we think about test-driven development as a tool, we recognize that like
all tools, it has times when it is effective and times when it is not. As my
father used to tell me, “You can put in a screw with a hammer, but it probably
isn’t the best choice.”</p>

<p>There are times that the developer begins with a clear picture of how a
component should work. There is a clear mental model of what is should do and
how to code it. Trying to baby-step into that design using a test-fist
technique would be tedious and time consuming. Nevertheless, the design
benefits from the TDD approach are still desirable. In these cases, it would be
nice if we could get the best of both worlds.</p>

<p>Test-driven development leads to these design benefits primarily by forcing the
developer into a rapid back-and-forth between consuming code (tests) and the
system under test. This, I believe, is where the real benefit comes from TDD.
Write a small amount of code, then a small amount of tests. Cycle back and
forth between them frequently. I call this short-cycle testing.</p>

<p>Tests are a tool that let you ask questions of your code. By asking questions
frequently, you can fine-tune the thing you are building. Sometimes you will
write a test first, sometimes after. Avoid staying in more mode too long. Keep
the back-and-forth fluid and frequent.</p>

<p>Even when you know what you want, switching to a test lets you confirm your
thinking. You will often find that the test tells you something you unexpected,
letting you correct it earlier. The tests will also help you find dependencies
and coupling you never realized were there. Trying to write tests after an
extended free-coding period will also tell you these things, but by then it
will be significantly harder to do anything about it.</p>

<h3 id="conclusion">Conclusion</h3>

<p>I said at the beginning that developers really want to be able to quickly write
the correct code</p>

<p>and know it does what they think it does. I have highlighted a few words in
that sentence that I think are the key elements. Developers want to be
efficient and do their work quickly. They also want to create the right thing.
Moreover, they need to be sure that the code they write does what they think it
does. This is the essential goal of developer testing and is what
differentiates it from other kinds of testing. </p>

<p>Developer tests are an effective tool that deliver quality and design benefits.
Test-driven development provides this result, but the biggest benefit comes not
from dogmatic application of TDD, but from using a short-cycle mode where you
write tests and code at almost the same time.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[HTML5 Frameworks]]></title>
    <link href="http://www.peterprovost.org//blog/2013/09/17/html5-frameworks/"/>
    <updated>2013-09-17T08:29:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2013/09/17/html5-frameworks</id>
    <content type="html"><![CDATA[<p>For a couple of weeks I&rsquo;ve been playing around with some of the updated tools I
use to make this blog. <a href="http://www.peterprovost.org//blog/2012/04/15/switching-the-blog-to-octopress/">Back in April 2012</a>, I pulled all of my
content out of a server-side ASP.NET blog engine and moved to
<a href="http://jekyllrb.com/">Jekyll</a> and <a href="http://octopress.org/">Octopress</a>. Honestly, I can&rsquo;t see myself
going back.</p>

<p>But it has been more than a year since I created the current skin, and it was
time for change. Also, Jekyll has matured a lot and many of the things
that Octopress brought to the table are no longer needed. So I decided to kill
two birds with one stone and update the whole thing&hellip; generator and skin.</p>

<p>Of course I want a responsive layout, and for a long time my go-to framework
has been Twitter Bootstrap. But TWBS has a few issues that have started to bug
me, most notably the way it handles font-sizes. So I decided to begin an investigation
of available frameworks and toolsets.</p>

<!-- More -->

<p>I&rsquo;m not sure if you&rsquo;ve tried searching for &ldquo;html5 template&rdquo;, but I will tell
you that it results in a a big list of &ldquo;free, fresh web design templates&rdquo;.
Nothing particular interesting or useful there. A few refining searches and clicks later
landed me at the <a href="https://github.com/usablica/front-end-frameworks">Front-end Frameworks repository</a> owned by usabli.ca. This is
the list the search engines were failing to provide for me.</p>

<p>You can clone the repository if you want, but since it is really just the code
for the compare website, I would recommend you star it instead (so you know
when changes happen) and then visit the 
<a href="http://usablica.github.io/front-end-frameworks/compare.html">CSS Front-end Frameworks Comparison</a> website itself.</p>

<p><img src="http://www.peterprovost.org//images/blog/2013-09-17-html5-frameworks/front-end-frameworks-compare.png" title="Screenshot" alt="CSS Front-end Frameworks Compare" /></p>

<p>As you can see, it gives you a nice list of the top frameworks, annotated with useful
bits like mobile/tablet support, browser support, license, etc. Great stuff and
certainly a link to keep handy.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Interviewed on Radio TFS]]></title>
    <link href="http://www.peterprovost.org//blog/2013/08/23/interviewed-on-radio-tfs/"/>
    <updated>2013-08-23T17:31:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2013/08/23/interviewed-on-radio-tfs</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://radiotfs.com/images/logo.png" title="Radio TFS Logo" /></p>

<p><strong>Update 2013-08-29:</strong> <a href="http://channel9.msdn.com/Shows/This+Week+On+Channel+9/TWC9-August-30-2013#time=9m21s">We got mentioned</a> in This Week on Channel 9 today. Woot!</p>

<p>This morning I did an interview on <a href="http://radiotfs.com/">Radio TFS</a>, hosted
by <a href="http://www.woodwardweb.com/">Martin Woodward</a> and <a href="http://coolthingoftheday.blogspot.com/">Greg Duncan</a>. The
topic was &ldquo;What have you been working on since Visual Studio 2012&rdquo;, and we had
a great time talking about all the cool stuff we&rsquo;ve done in the VS2012 updates
and what we&rsquo;re targeting for Visual Studio 2013.</p>

<p>You can download &amp; listen to the interview here:<br />
<a href="http://radiotfs.com/Show/64/PeterProvostonVisualStudio2013Ultimate">Episode 64: Peter Provost on Visual Studio 2013 Ultimate</a></p>

<p>Many thanks to Martin and Greg for having me. It was fun and I&rsquo;m looking
forward to doing it again so we can talk more about developer testing.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Visual Studio 2012 Fakes - Part 3 - Observing Stub Behavior]]></title>
    <link href="http://www.peterprovost.org//blog/2012/11/29/visual-studio-2012-fakes-part-3/"/>
    <updated>2012-11-29T12:04:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/11/29/visual-studio-2012-fakes-part-3</id>
    <content type="html"><![CDATA[<p>This year at both TechEd North America and TechEd Europe I gave a presentation
called &ldquo;Testing Untestable Code with Visual Studio Fakes&rdquo;. So far VS Fakes has
been very well received by customers, and most people seemed to understand my
feelings about when (and when not)  to use Shims (see <a href="http://www.peterprovost.org//blog/2012/04/15/visual-studio-11-fakes-part-1/">Part 2</a> for more on
this). But one thing that has consistently come up has been questions about
Behavioral Verification.</p>

<p>I talked about this briefly in <a href="http://www.peterprovost.org//blog/2012/04/25/visual-studio-11-fakes-part-2/">Part 1</a> of this series, but let me rehash a
few of the important points:</p>

<ul>
  <li>Stubs are dummy implementations of interfaces or abstract classes that you
use while unit testing to provide concrete, predictable instances to fulfill
the dependencies of your system under test.</li>
  <li>Mocks are Stubs that provide the ability to verify calls on the Stub,
typically including things like the number of calls made, the arguments
passed in, etc.</li>
</ul>

<p>With Visual Studio Fakes, introduced in Visual Studio 2012 Ultimate, we are
providing the ability to generate fast running, easy to use Stubs, but they are
<strong>not</strong> Mocks.  They do not come with any kind of behavioral verification built
in. But as I showed at TechEd Europe, there are hooks available in the
framework that allow one to perform this kind of verification. This post will
show you how they work and how to use them to create your own Mocks.</p>

<!-- More -->

<h2 id="why-would-you-need-to-verify-stub-calls">Why would you need to verify stub calls?</h2>

<p>There is a long-running philosophical argument between the &ldquo;mockists&rdquo; and the
&ldquo;classists&rdquo; about whether mocking is good or bad. My personal take is that they
can be very useful when unit testing certain kinds of code, but also that
they can cause problems if overused, because rather than pinning down the
external behavior of a method, they pin the implementation. But rather than
dwell on that, lets look at some of the cases where they are valuable. </p>

<p>Suppose you have a class who&rsquo;s responsibility is to coordinate calls to other
classes. These might be classes like message brokers, delegators, loggers, etc.
The whole purpose of this class is to make predictable, coordinated calls on
other objects. That is the external behavior we want to confirm.</p>

<p>Consider a system like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>System Under Test</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="c1">// ILogSink will be implemented by the various targets to which</span>
</span><span class="line"><span class="c1">// log messages can be sent.</span>
</span><span class="line"><span class="k">public</span> <span class="k">interface</span> <span class="n">ILogSink</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">void</span> <span class="nf">LogMessage</span><span class="p">(</span><span class="kt">string</span> <span class="n">message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">categories</span><span class="p">,</span> <span class="kt">int</span> <span class="n">priority</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="c1">// MessageLogger is a service class that will be used to log the</span>
</span><span class="line"><span class="c1">// messages sent by the application. Only the method signatures</span>
</span><span class="line"><span class="c1">// are shown, since in this case we really don&#39;t need to care</span>
</span><span class="line"><span class="c1">// about the implementation.</span>
</span><span class="line"><span class="k">public</span> <span class="k">class</span> <span class="nc">MessageLogger</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="nf">RegisterMessageSink</span><span class="p">(</span><span class="n">ILogSink</span> <span class="n">messageSink</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="nf">LogMessage</span><span class="p">(</span><span class="kt">string</span> <span class="n">message</span><span class="p">);</span>
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="nf">LogMessage</span><span class="p">(</span><span class="kt">string</span> <span class="n">message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">categories</span><span class="p">);</span>
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="nf">LogMessage</span><span class="p">(</span><span class="kt">string</span> <span class="n">message</span><span class="p">,</span> <span class="kt">string</span> <span class="n">categories</span><span class="p">,</span> <span class="kt">int</span> <span class="n">priority</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>What we need to confirm is that the right calls are made into the registered
sinks, and that all the parameters are passed in correctly. When we use Stubs
to test a class like that, we will be providing fake versions of the <code>ILogSink</code>
so we should be able to have the fake version tell us how it was called.</p>

<h2 id="behavioral-verification-using-closures">Behavioral verification using closures</h2>

<p>I showed in my previous posts how you can combine lambda expressions and
closures to pass data out of a stub&rsquo;s method delegate. For this test, I
will do this again to verify that the sink is being called.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Testing that the sink is called</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">VerifyOneSinkIsCalledCorrectly</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="c1">// Arrange</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MessageLogger</span><span class="p">();</span>
</span><span class="line">    <span class="n">var</span> <span class="n">wasCalled</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">LogMessageStringStringInt32</span> <span class="p">=</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">cat</span><span class="p">,</span> <span class="n">pri</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">wasCalled</span> <span class="p">=</span> <span class="k">true</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">LogMessage</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(</span><span class="n">wasCalled</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This works, and isn&rsquo;t really too verbose. But as I start to test more things,
it can become a bit cluttered with complex setup code.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Testing that multiple sinks are called</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">VerifyMultipleSinksCalledCorrectly</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="c1">// Arrange</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MessageLogger</span><span class="p">();</span>
</span><span class="line">    <span class="n">var</span> <span class="n">wasCalled1</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">wasCalled2</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">wasCalled3</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink1</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">LogMessageStringStringInt32</span> <span class="p">=</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">cat</span><span class="p">,</span> <span class="n">pri</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">wasCalled1</span> <span class="p">=</span> <span class="k">true</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink2</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">LogMessageStringStringInt32</span> <span class="p">=</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">cat</span><span class="p">,</span> <span class="n">pri</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">wasCalled2</span> <span class="p">=</span> <span class="k">true</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink3</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">LogMessageStringStringInt32</span> <span class="p">=</span> <span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="n">cat</span><span class="p">,</span> <span class="n">pri</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">wasCalled3</span> <span class="p">=</span> <span class="k">true</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink1</span><span class="p">);</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink2</span><span class="p">);</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink3</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">LogMessage</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(</span><span class="n">wasCalled1</span><span class="p">);</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(</span><span class="n">wasCalled2</span><span class="p">);</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(</span><span class="n">wasCalled3</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>As I add more tests to verify the parameters, it can get out of hand pretty
quickly.  Closures are great when I&rsquo;m checking only a few things, but the
more I want to track, the harder it gets.</p>

<p>What I really need is a better way to track these calls. </p>

<h2 id="introducing-iinstanceobserver">Introducing IInstanceObserver</h2>

<p>When we were creating the Stubs framework, we knew these situations would come up. We also
knew that people can get very passionate about the syntax and form that their
mocking frameworks use. So we decided to introduce an extension point into the
generated Stubs that enables people to create any kind of mocking or
verification API.</p>

<p>Every Stub that Visual Studio generate has a property on it called
<code>InstanceObserver</code> that can take any object which implements the
<code>IStubObserver</code> interface.  When an observer is installed into a Stub, it will
be called every time a method or property on the Stub is accessed. This is what
you really need to do the kind of behavioral verification I need here.</p>

<p>The definition of the interface <code>IInstanceObserver</code> is pretty simple:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>IInstanceObserver interface</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">interface</span> <span class="n">IStubObserver</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">);</span>
</span><span class="line">   <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg1</span><span class="p">);</span>
</span><span class="line">   <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg1</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg2</span><span class="p">);</span>
</span><span class="line">   <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg1</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg2</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg3</span><span class="p">);</span>
</span><span class="line">   <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="k">params</span> <span class="kt">object</span><span class="p">[]</span> <span class="n">args</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p><em>The reason there are five overloads is an optimization based on the
observation that most methods in .NET have three or fewer arguments. The final
overload is used for those that exceed three. This is a common pattern in the
CLR and .NET BCL.</em></p>

<p>The first parameter to each call is the type of the interface that was stubbed. 
The second parameter is a delegate that represents the call which was made on
the interface. The remaining parameters are the arguments provided to the call.</p>

<p>The delegate is properly typed so even if the interface or method is generic,
the <code>MethodInfo</code> provided in the <code>stubCall</code> delegate will have the types that
were actually used when the object was called.</p>

<h2 id="creating-a-custom-stubobserver">Creating a custom StubObserver</h2>

<p>Using this interface, I can create a class that will record the calls
made to my stub.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>CustomObserver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">class</span> <span class="nc">CustomObserver</span> <span class="p">:</span> <span class="n">IStubObserver</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">List</span><span class="p">&lt;</span><span class="n">MethodInfo</span><span class="p">&gt;</span> <span class="n">calls</span> <span class="p">=</span> <span class="k">new</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">MethodInfo</span><span class="p">&gt;();</span>
</span><span class="line">
</span><span class="line">    <span class="k">public</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">MethodInfo</span><span class="p">&gt;</span> <span class="n">GetCalls</span><span class="p">()</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="k">return</span> <span class="n">calls</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="k">public</span> <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="k">params</span> <span class="kt">object</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">calls</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">stubCall</span><span class="p">.</span><span class="n">Method</span><span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="k">public</span> <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg1</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg2</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg3</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="k">this</span><span class="p">.</span><span class="n">Enter</span><span class="p">(</span><span class="n">stubbedType</span><span class="p">,</span> <span class="n">stubCall</span><span class="p">,</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">arg3</span> <span class="p">});</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="k">public</span> <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg1</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg2</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="k">this</span><span class="p">.</span><span class="n">Enter</span><span class="p">(</span><span class="n">stubbedType</span><span class="p">,</span> <span class="n">stubCall</span><span class="p">,</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span> <span class="p">});</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="k">public</span> <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">,</span> <span class="kt">object</span> <span class="n">arg1</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="k">this</span><span class="p">.</span><span class="n">Enter</span><span class="p">(</span><span class="n">stubbedType</span><span class="p">,</span> <span class="n">stubCall</span><span class="p">,</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span> <span class="n">arg1</span> <span class="p">});</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="k">public</span> <span class="k">void</span> <span class="nf">Enter</span><span class="p">(</span><span class="n">Type</span> <span class="n">stubbedType</span><span class="p">,</span> <span class="n">Delegate</span> <span class="n">stubCall</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="k">this</span><span class="p">.</span><span class="n">Enter</span><span class="p">(</span><span class="n">stubbedType</span><span class="p">,</span> <span class="n">stubCall</span><span class="p">,</span> <span class="k">new</span> <span class="kt">object</span><span class="p">[]</span> <span class="p">{</span> <span class="p">}</span> <span class="p">);</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p><em>Yes, I realize that this implementation spoils the whole point of the overloads
on IStubObserver, but we&rsquo;ll get rid of that later.</em></p>

<p>Now we can rewrite two tests above without using closures. The observer
will do the tracking for us, and we can simply check what it saw after
we make our call to the system under test.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Testing with the CustomObserver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">VerifyOneSinkIsCalledCorrectly</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MessageLogger</span><span class="p">();</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sink</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">   <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">sut</span><span class="p">.</span><span class="n">LogMessage</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">CustomObserver</span><span class="p">)</span><span class="n">sink</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">mi</span> <span class="p">=&gt;</span> <span class="n">mi</span><span class="p">.</span><span class="n">Name</span><span class="p">.</span><span class="n">Contains</span><span class="p">(</span><span class="s">&quot;ILogSink.LogMessage&quot;</span><span class="p">)));</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">VerifyMultipleSinksCalledCorrectly</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MessageLogger</span><span class="p">();</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sink1</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sink2</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sink3</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">   <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink1</span><span class="p">);</span>
</span><span class="line">   <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink2</span><span class="p">);</span>
</span><span class="line">   <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink3</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">sut</span><span class="p">.</span><span class="n">LogMessage</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">CustomObserver</span><span class="p">)</span><span class="n">sink1</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">mi</span> <span class="p">=&gt;</span> <span class="n">mi</span><span class="p">.</span><span class="n">Name</span><span class="p">.</span><span class="n">Contains</span><span class="p">(</span><span class="s">&quot;ILogSink.LogMessage&quot;</span><span class="p">)));</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">CustomObserver</span><span class="p">)</span><span class="n">sink2</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">mi</span> <span class="p">=&gt;</span> <span class="n">mi</span><span class="p">.</span><span class="n">Name</span><span class="p">.</span><span class="n">Contains</span><span class="p">(</span><span class="s">&quot;ILogSink.LogMessage&quot;</span><span class="p">)));</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">CustomObserver</span><span class="p">)</span><span class="n">sink3</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">mi</span> <span class="p">=&gt;</span> <span class="n">mi</span><span class="p">.</span><span class="n">Name</span><span class="p">.</span><span class="n">Contains</span><span class="p">(</span><span class="s">&quot;ILogSink.LogMessage&quot;</span><span class="p">)));</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>While we&rsquo;re not using the closure anymore, those asserts are pretty ugly, so we
will want to look at fixing that. But before we do, I want to delete some code.</p>

<h2 id="using-the-built-in-stubobserver-class">Using the built-in StubObserver class</h2>

<p>It turns out that VS2012 include an implementation of <code>IStubObserver</code> in the
framework that does everything my implementation above does, but it also
includes all the missing stuff like the arguments, etc. This class is called
<code>StubObserver</code> and is in the <code>Microsoft.QualityTools.Testing.Fakes.Stubs</code>
namespace.</p>

<p>If we swap out my <code>CustomObserver</code> for the built-in <code>StubObserver</code>, the
resulting test code is very similar, with just a few changes to handle how
<code>StubObserver</code> provides the method call data back to us.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Testing with StubObserver</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">VerifyOneSinkIsCalledCorrectly</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="c1">// Arrange</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MessageLogger</span><span class="p">();</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">LogMessage</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">StubObserver</span><span class="p">)</span><span class="n">sink</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">call</span> <span class="p">=&gt;</span> <span class="n">call</span><span class="p">.</span><span class="n">StubbedMethod</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="s">&quot;LogMessage&quot;</span><span class="p">));</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">VerifyMultipleSinksCalledCorrectly</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="c1">// Arrange</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">MessageLogger</span><span class="p">();</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink1</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink2</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">sink3</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubILogSink</span> <span class="p">{</span> <span class="n">InstanceObserver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubObserver</span><span class="p">()</span> <span class="p">};</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink1</span><span class="p">);</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink2</span><span class="p">);</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">RegisterMessageSink</span><span class="p">(</span><span class="n">sink3</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">sut</span><span class="p">.</span><span class="n">LogMessage</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">StubObserver</span><span class="p">)</span><span class="n">sink1</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">call</span> <span class="p">=&gt;</span> <span class="n">call</span><span class="p">.</span><span class="n">StubbedMethod</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="s">&quot;LogMessage&quot;</span><span class="p">));</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">StubObserver</span><span class="p">)</span><span class="n">sink2</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">call</span> <span class="p">=&gt;</span> <span class="n">call</span><span class="p">.</span><span class="n">StubbedMethod</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="s">&quot;LogMessage&quot;</span><span class="p">));</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsTrue</span><span class="p">(((</span><span class="n">StubObserver</span><span class="p">)</span><span class="n">sink3</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">).</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">call</span> <span class="p">=&gt;</span> <span class="n">call</span><span class="p">.</span><span class="n">StubbedMethod</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="s">&quot;LogMessage&quot;</span><span class="p">));</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="simplifying-the-assertions">Simplifying the Assertions</h2>

<p>While that works, the Assert calls are certainly not friendly to the eye. What we&rsquo;d really like is
something more like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="n">Verify</span><span class="p">.</span><span class="n">MethodCalled</span><span class="p">(</span> <span class="n">sink1</span><span class="p">,</span> <span class="s">&quot;LogMessage&quot;</span> <span class="p">);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>It turns out that making this helper method isn&rsquo;t very hard.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Verify Helper Class</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">static</span> <span class="k">class</span> <span class="nc">Verify</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="n">MethodCalled</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;(</span> <span class="n">IStub</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span> <span class="n">stub</span><span class="p">,</span> <span class="kt">string</span> <span class="n">methodName</span> <span class="p">)</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="n">var</span> <span class="n">observer</span> <span class="p">=</span> <span class="n">stub</span><span class="p">.</span><span class="n">InstanceObserver</span><span class="p">;</span>
</span><span class="line">      <span class="k">if</span> <span class="p">(</span><span class="n">observer</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class="line">         <span class="k">throw</span> <span class="k">new</span> <span class="nf">ArgumentException</span><span class="p">(</span><span class="s">&quot;stub&quot;</span><span class="p">,</span> <span class="s">&quot;No InstanceObserver installed into the stub.&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">      <span class="n">var</span> <span class="n">wasCalled</span> <span class="p">=</span> <span class="n">observer</span><span class="p">.</span><span class="n">GetCalls</span><span class="p">().</span><span class="n">Any</span><span class="p">(</span><span class="n">call</span> <span class="p">=&gt;</span> <span class="n">call</span><span class="p">.</span><span class="n">StubbedMethod</span><span class="p">.</span><span class="n">Name</span> <span class="p">==</span> <span class="n">methodName</span><span class="p">);</span>
</span><span class="line">      <span class="k">if</span> <span class="p">(</span><span class="n">wasCalled</span> <span class="p">==</span> <span class="k">false</span><span class="p">)</span>
</span><span class="line">         <span class="k">throw</span> <span class="k">new</span> <span class="nf">VerificationException</span><span class="p">(</span><span class="s">&quot;Method {0} was expected, but was not called&quot;</span><span class="p">,</span> <span class="n">methodName</span><span class="p">);</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>That isn&rsquo;t too bad. Still not perfect because I really don&rsquo;t like the string for
the method name because it won&rsquo;t be refactoring resilient. Fixing that will take
us on a trek through the world of Linq expressions however, which I will cover in
a later post.</p>

<h2 id="conclusions">Conclusions</h2>

<p>While the VS 2012 Fakes framework does not have a built-in verification framework, you can do
verification using existing language constructs like closures and lambdas. You also can leverage
the <code>IStubObserver</code> interface to create a more customized behavioral frameworks, potentially
going all the way to a full fluent API for &ldquo;mockist&rdquo; style behavioral verification.</p>

<p>If anything, the assert statements have gotten uglier, but we have now
eliminated all of the closures, and moves all of the verification logic to the
Assert section of the test. We&rsquo;re making headway.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My path to Inbox Zero]]></title>
    <link href="http://www.peterprovost.org//blog/2012/10/11/my-path-to-zero-inbox/"/>
    <updated>2012-10-11T00:15:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/10/11/my-path-to-zero-inbox</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-10-08-my-path-to-zero-inbox/pile-of-email.jpg" /></p>

<p><em>I know this post is probably going to make a lot of people say, &ldquo;Holy crap, man.
If you need that much of a system, you get too much email.&rdquo; All I can say is
&ldquo;Guilty as charged, but I know I&rsquo;m not the only one with this problem.&rdquo; So if you
find this useful, great. If not, move on.</em></p>

<p>I&rsquo;ve long been a fan of the whole <a href="http://inboxzero.com/">Inbox Zero</a> idea. While the concept of
using other kinds of task lists (e.g. Outlook Tasks, Trello or Personal Kanban)
is nice, in my experience the Inbox is a much more natural place to keep track
of the things I need to do. Like it or not, a large number of us in the tech
sector use email as our primary personal project management system. I don&rsquo;t
think this is just a &ldquo;Microsoft PM&rdquo; thing, but certainly the amount of email
that happens here, in this role, makes it more the case.</p>

<!-- more -->

<p>Scott Hanslelman has written a few times about his rule system on his blog. In
<a href="http://www.hanselman.com/blog/TheThreeMostImportantOutlookRulesForProcessingMail.aspx">his most recent post on this topic</a>, he classifies emails into four big
buckets:</p>

<ol>
  <li>Most Important - I am on the To line</li>
  <li>Kind of Important - I am CC</li>
  <li>External - From someone outside the company</li>
  <li>Meeting invites</li>
</ol>

<p>I tried his system for a while, but I found that I still ended up with too
many emails in my Inbox that weren&rsquo;t high priority for me to read, but that
I may need to find later via search.</p>

<p>This brings up a good point: My system depends on the idea that I will
<a href="http://www.peterprovost.org/blog/2006/07/03/My-Outlook-TRAF-System/">TRAF</a> (Toss, Refer, Act, File) the things in my Inbox quickly. Any email
which doesn&rsquo;t meet certain criteria (more on that in a second) will be
<strong>F</strong>iled by default, and if I need to I can find them later with search.</p>

<h2 id="naming-rules">Naming rules</h2>

<p>My system leverages the power of Inbox rules. I use Outlook, but most email
clients (web or local) have something like them, but you may need to adjust the
specifics to your client. This system also ends up with a lot of single purpose
rules. I like this because it lets me see in the rules management UI what I
have. I tried the great big &ldquo;Mailing Lists&rdquo; rule before and it was difficult to
keep track of. </p>

<p>Naming the rules is an important part of keeping track of it all. My general
rule naming pattern is like this:</p>

<p><code>&lt;action&gt; - &lt;criteria&gt;</code></p>

<p>Action is typically things like &ldquo;Move&rdquo; or &ldquo;Don&rsquo;t move&rdquo;. Criteria is typically
things like &ldquo;to:(some mailing list)&rdquo;. I tend to use the descriptive name of
things in the rule name rather than the email address or alias name. This,
again, helps me see at a glance my rules and the priority stack.</p>

<h2 id="how-does-it-work">How does it work?</h2>

<p>As I said, I do this in Outlook, so it is important to understand that Outlook
will process the rules in order, from top to bottom, only stopping if a rule
says explcitly to stop.</p>

<p>The essence of this system is the final rule in the stack which moves
everything to an archive folder, and then the rules before which pick out what
they care about and stop the processing chain.</p>

<p>In Outlook, I do this with the &ldquo;Stop Processing More Rules&rdquo; action that has no
conditions (i.e. it matches all arriving email). To make this rule, you can&rsquo;t
use the simple rules creation UI. Instead click Advanced Options. Leave
everything in the conditions section blank and click Next. It will warn you
about the rule being applied to all messages, and since this is what we want,
click Yes. Then in the Actions section (2nd page) near the bottom, check &ldquo;stop
processing more rules&rdquo;. It should look something like this:</p>

<p><img class="center" src="http://www.peterprovost.org//images/blog/2012-10-08-my-path-to-zero-inbox/Stop-Processing.png" title="'Outlook Rules Example Showing Stop Processing Action'" /></p>

<h2 id="rule-priorities">Rule Priorities</h2>

<p>Over time I&rsquo;ve learned that my rules come in a prioritized set of groups. </p>

<p>Each rule takes a look at the mail and makes a simple decision: keep it in the
Inbox, move it somewhere, or pass and let the next rule give it a try. This
means that it is worth putting a bit of thought into the priority of the rules
so you get expected outcomes from the tool.</p>

<p>My groups, in priority order and with an example or two, are listed below. The
final &ldquo;group&rdquo; only has the catch-all move rule I mentioned above. </p>

<h3 id="pre-processing">1. Pre-processing</h3>

<p>These rules are about classification and other modifications to emails as they
come in. Unlike all the other rules, these typically <strong>do not</strong> end with the
&ldquo;Stop Processing More Rules&rdquo; instruction, but apply things like Tags,
Categories, Importance, etc.</p>

<p>Examples:</p>

<ul>
  <li><strong>Tag Personal - from:(family)</strong> - I don&rsquo;t use this right now, but if I
wanted to color code, tag or categorize mails from my family, I would do this
here. </li>
  <li><strong>Tag Important - from:(my management)</strong> - Same idea, but maybe you want
emails from your bosses to be red.</li>
</ul>

<h3 id="high-priority---always-read-these">2. High Priority - Always read these</h3>

<p>These rules are the first set that actually make a decision on an email, in
this case looking for emails that I will always see in my Inbox. Any criteria
that I come up with which means to me &ldquo;always read these, no matter what&rdquo; goes
in this bucket. Currently, I have only two rules in this bucket. </p>

<p>Examples:</p>

<ul>
  <li><strong>Don&rsquo;t move - to:(me)</strong> - This is the most important rule in my system, and
typically is the second rule you create after the ending catch-all.  If I&rsquo;m
on the To: line explicitly, don&rsquo;t move it and stop processing more rules.</li>
  <li><strong>Don&rsquo;t move - Meeting invites</strong> - I added this one after I realized that
sometimes in my working group people will send meeting invites to one of the
distribution or security groups I&rsquo;m in, which would cause them to be caught
by rules in the next category, and I would miss them.</li>
</ul>

<h3 id="low-priority---move-mailing-lists">3. Low Priority - Move mailing lists</h3>

<p>I like to have my mailing lists taken out of my Inbox and I will scan them, 
search them, or read them when I have the time or inclination.</p>

<p>To do this, I have a root folder called &ldquo;Mailing Lists&rdquo; with subfolders
underneath. The rules in this section typically look for something in the To
line to figure out where it was sent and then move it to the appropriate
folder.  (Note that some lists may need you to look in the From or the Subject
to identify it.)</p>

<p>I create one rule for each target folder, which means that sometimes they may
combine different lists together because they mean the same thing (logically)
to me. An example might be if there was a TDD list and a Unit Testing list.
Odds are I would move those both to the same target folder.</p>

<p>Depending on how many mailing lists you subscribe to, this section may be big
or small. </p>

<p>Examples:</p>

<ul>
  <li><strong>Move - to:(Internal Agile Dscussion Lists)</strong></li>
  <li><strong>Move - to:(Some other discussion list)</strong> </li>
</ul>

<h3 id="special-cc-handlers">4. Special CC handlers</h3>

<p>If an email gets this far down in the rule chain, I was likely on the CC or BCC
or it was sent to a distribution list that I am on, that I don&rsquo;t have a rule
for. There are actually a number of cases where I do want to see an email that
gets this far, so I have special rules for those which each result in &ldquo;Stop
Processing More Rules&rdquo; to keep the message in the Inbox.</p>

<ul>
  <li><strong>Don&rsquo;t move - from:(my management chain)</strong> - This rule includes all of the
people I consider in my management chain, all the way up to Mr. Ballmer. It
also includes people who aren&rsquo;t directly in my chain of command, but who I
consider to have equivalent importance in my email triaging. I&rsquo;ve often
struggled with whether to put this one in the upper Don&rsquo;t Move section or
not. However, since we have a few managers here who can be VERY active on
some of the mailing lists, putting it up there can cause it to fill your
Inbox with stuff you might not really need to read. Most of these managers
have learned, however, that if you really want someone to read something, you
explicitly list them on the To line, so the rule I already have up there
works.</li>
  <li><strong>Don&rsquo;t move - from:(my direct reports)</strong> - Now that I&rsquo;m a manager again, I
like to see what my folks are saying (not as Big Brother, more as a coach).
This rule lets me see mails where they CC me. If I wanted to see everything
they sent, I would move this one up to the upper &ldquo;Don&rsquo;t Move&rdquo; section.</li>
  <li><strong>Don&rsquo;t move - from:(me)</strong> - This is a weird one, and I&rsquo;m not sure I really
need it, but it does sometimes help me find distribution lists that I need a
rule for.</li>
  <li><strong>Don&rsquo;t move - from:(family)</strong> - My wife, kids, sister, brothers- and
sisters-in-law, parents, etc.</li>
  <li><strong>Don&rsquo;t move - from:(friends)</strong> - Other friends and colleagues whose messages
I will read even if I&rsquo;m on the CC.</li>
  <li><strong>Don&rsquo;t move - from:(other lists)</strong> - A few other low-frequency mailing lists
used internally that don&rsquo;t warrant their own folder.</li>
</ul>

<h3 id="catch-all-move">5. Catch-all move</h3>

<ul>
  <li><strong>Move everything to archive</strong> - This is the catch-all rule discussed above.
 It should always be last in the list.</li>
</ul>

<h2 id="the-results">The results</h2>

<p>This system has allowed me to regain control of my Inbox, and only occasionally
do I miss something important.  I have 32 rules in my system right now.  Most
of them are split evenly between #3 and #4, mostly because I like the
granularity of having each different thing in its own rule.</p>

<p>The downside to this is that I really depend on people to know the different
between To (read this) and CC (FYI) when they send an email.  A lot of people,
and a lot of email clients (e.g. Gmail), really don&rsquo;t get this and love to put
everyone but the specific person you&rsquo;re replying to on the CC line. This is
really a bummer because it takes a field which has meaning and degrades it to
being the same thing as the To field.</p>

<p>Anyway, I&rsquo;m not sure if this will be useful to anyone else but those of us who
regularly get hundreds of emails a day, but if you do find it useful let me
know!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Introducing Visual Studio 2012 (Video)]]></title>
    <link href="http://www.peterprovost.org//blog/2012/10/01/introducing-visual-studio-2012-video/"/>
    <updated>2012-10-01T10:00:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/10/01/introducing-visual-studio-2012-video</id>
    <content type="html"><![CDATA[<p>Near the end of the development cycle for Visual Studio 2012, a group of folks
in the VSALM team (led by my very creative manager Tracey Trewin) came up with
this cool animated video introducing some of the great new features in Visual
Studio 2012 Ultimate. I think it is pretty cool, and even pretty funny, so I
wanted to share it with you all.</p>

<div class="embed-video-container"><iframe src="http://www.youtube.com/embed/LHhp5pEuSMo "></iframe></div>

<p><br />
What do you think?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Adding Ninject to Web API]]></title>
    <link href="http://www.peterprovost.org//blog/2012/06/19/adding-ninject-to-web-api/"/>
    <updated>2012-06-19T20:21:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/06/19/adding-ninject-to-web-api</id>
    <content type="html"><![CDATA[<p>In my last post I focused on how to unit test a new Visual Studio 2012 RC
ASP.NET Web API project. In general, it was pretty straightforward, but when I
had Web API methods that needed to return an <code>HttpResponseMessage</code>, it got a
little harder.</p>

<p>If you recall, I decided to start with the 
<a href="http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations">Creating a Web API that Supports CRUD Operations </a> tutorial and the
<a href="http://code.msdn.microsoft.com/ASP-NET-Web-API-Tutorial-c4761894">provided solution</a> that came with it.  That project did not use any form of
dependency inversion to resolve the controller&rsquo;s need for a
<code>ProductRepository</code>.  My solution in that post was to use manual dependency
injection and a default value. But in the real world I would probably reach for
a dependency injection framework to avoid having to do all the resolution
wiring throughout my code.</p>

<p>In this post I am going to convert the manual injection I used in the last post
to one that uses the <a href="http://www.ninject.org/">Ninject framework</a>. Of course you can use any other
framework you wanted like <a href="http://unity.codeplex.com/">Unity</a>, <a href="http://docs.castleproject.org/Default.aspx?Page=MainPage&amp;NS=Windsor&amp;AspxAutoDetectCookieSupport=1">Castle Windsor</a>, <a href="http://docs.structuremap.net/">StructureMap</a>,
etc.  but the code that adapts between it and ASP.NET Web API will probably
have to be different.</p>

<!-- more -->

<h2 id="getting-started">Getting Started</h2>

<p>First let&rsquo;s take a look at the code I had for the <code>ProductsController</code> at the end
of the last post, focusing on the constructors and the <code>IProductRepository</code> field.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Manual Depdendency Injection</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">namespace</span> <span class="nn">ProductStore.Controllers</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">public</span> <span class="k">class</span> <span class="nc">ProductsController</span> <span class="p">:</span> <span class="n">ApiController</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="k">readonly</span> <span class="n">IProductRepository</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">ProductsController</span><span class="p">()</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductRepository</span><span class="p">();</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">ProductsController</span><span class="p">(</span> <span class="n">IProductRepository</span> <span class="n">repository</span> <span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">repository</span> <span class="p">=</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Everything else stays the same</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>The default constructor provides the default value for the repository, while
the second constructor lets us provide one. This is fine when we only have a
single controller, but in a real-world system we will likely have a number of
different controllers, and having the logic for which repository to use spread
among all those controllers is going to be a nightmare to maintain or change.</p>

<p>By default, the ASP.NET Web API routing stuff will use the default constructor
to create the controller. What I want to do is make that default constructor go
away, and instead let Ninject be responsible for providing the required
dependency.</p>

<h2 id="a-quick-aside---constructor-injection-or-property-injection">A quick aside - Constructor injection or Property injection?</h2>

<p>This came up in one of my talks last week at TechEd, so it probably warrants
some discussion here. When <a href="http://bradwilson.typepad.com/">Brad Wilson</a> and I made the first version of the
ObjectBuilder engine that hides inside Unity and the various P&amp;P CAB
frameworks, we got to have this argument with people all the time.</p>

<p>While this argument looks like another one of those &ldquo;philosophical arguments&rdquo;
that doesn&rsquo;t have a right answer, I don&rsquo;t think it really is. I think the
distinction between constructor injection and property injection is important,
and I think you can find yourself using both depending on the circumstances.</p>

<p>Here&rsquo;s the gist of my argument: If the class would be in an invalid state
without the dependency, then it is a <strong>hard dependency</strong> and should be resolved
via constructor injection. It cannot be used without it. Putting the dependency
on the constructor and not providing a default constructor makes it very clear
to the developer who wants to consume this class. The developer is required to
provide the dependency or the class cannot be created. If you find yourself
doing a null check everywhere the dependency gets used, and especially if you
throw an exception when it is null, then you likely have a hard dependency.</p>

<p>But if the class has a dependency that either isn&rsquo;t required, or that will
use a default object or a null object if it is not provided, then it is a
<strong>soft dependency</strong> and should not be resolved via constructor injection. If
you do choose to express this optional dependency via constructor, then you 
should either show the default value in the constructor definition or provide
an override that doesn&rsquo;t require it. Then if the developer wants to provide
a special implementation (logging is an example that comes to mind), they 
can provide it via the property setter.</p>

<p>Dependency injection containers hide a lot of this from you, which might take
you to a &ldquo;who cares&rdquo; kind of place. But one thing I always look for in DI
frameworks is that I don&rsquo;t have to change the code itself very much. It should
read and be understandable without knowing anything about the DI container.  If
I don&rsquo;t want to use a DI framework at all, the code should still express its
meaning and it should still be usable. I believe that this distinction between
<strong>hard</strong> and <strong>soft</strong> dependencies makes that clear.</p>

<h2 id="expressing-the-iproductrepository-dependency">Expressing the IProductRepository dependency</h2>

<p>In our case, the repository is a <strong>hard dependency</strong>, so I will express it on
the constructor. Additionally, I don&rsquo;t actually want the default object instance
so I&rsquo;m going to delete the default constructor. It will be invalid to create a 
<code>ProductsController</code> without providing an instance of <code>IProductRepository</code>.</p>

<p>The code now looks like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Manual Depdendncy Injection ProductsController.cs</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">namespace</span> <span class="nn">MvcApplication.Controllers</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">public</span> <span class="k">class</span> <span class="nc">ProductsController</span> <span class="p">:</span> <span class="n">ApiController</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="k">readonly</span> <span class="n">IProductRepository</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">ProductsController</span><span class="p">(</span><span class="n">IProductRepository</span> <span class="n">repository</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">repository</span> <span class="p">=</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Everything else stays the same</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>If you try to run the app now, either through the web page that consumes
the API or by pointing a browser at **<your local="" app="" url="">/api/products** 
you will get an HTTP 500 Internal Server error because the ASP.NET routing
cannot create an instance of the controller.</your></p>

<p>It is time to bring Ninject to the party.</p>

<h2 id="using-ninject-to-resolve-the-repository-dependency">Using Ninject to resolve the repository dependency</h2>

<h3 id="getting-the-nuget-packages">Getting the NuGet Packages</h3>

<p>In previous releases of ASP.NET MVC4, you were able to use the existing
<strong>Ninject MVC3</strong> NuGet package to give us all the required glue code. But with
the RC build of MVC4, it no longer works because the MVC team changed the
dependency resolution mechanism for Web API projects. </p>

<p>Since I can&rsquo;t use the <strong>Ninject MVC3</strong> package, I need to do something slightly
different. Instead, I will use the <strong>Ninject.Web.Common</strong> package and then
create my own wrapper class to adapt between the <code>IDependencyResolver</code> API and
Ninject.</p>

<p>When you add the <strong>Ninject.Web.Common</strong> NuGet package to your project, it does
a few things to help you. So that you can skip adding a bunch of code to your
Global.asax.cs file, it uses a nice little package called <strong>WebActivator</strong>
to call another class when certain ASP.NET events have happened.</p>

<p>After adding <strong>Ninject.Web.Common</strong> to the project, open the App_Start folder
and you will see a new file called NinjectWebCommon. This static class will be
called by WebActivator when the application starts and stops. The Ninject.Web.Common
package provided it with the code required to bootstrap the Ninject kernel.</p>

<h3 id="ninjectdependencyresolver">NinjectDependencyResolver</h3>

<p>I said we needed an implementation of <code>IDependencyResolver</code> that knew about
Ninject. Fortunately my good friend Brad Wilson came to the rescue again, and
pointed me to <a href="https://gist.github.com/2417226">a chunk of code he&rsquo;d written to do just that</a>. Here&rsquo;s my
slightly modified version of his code:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>NinjectDependencyResolver.cs</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
<span class="line-number">40</span>
<span class="line-number">41</span>
<span class="line-number">42</span>
<span class="line-number">43</span>
<span class="line-number">44</span>
<span class="line-number">45</span>
<span class="line-number">46</span>
<span class="line-number">47</span>
<span class="line-number">48</span>
<span class="line-number">49</span>
<span class="line-number">50</span>
<span class="line-number">51</span>
<span class="line-number">52</span>
<span class="line-number">53</span>
<span class="line-number">54</span>
<span class="line-number">55</span>
<span class="line-number">56</span>
<span class="line-number">57</span>
<span class="line-number">58</span>
<span class="line-number">59</span>
<span class="line-number">60</span>
<span class="line-number">61</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">using</span> <span class="nn">System</span><span class="p">;</span>
</span><span class="line"><span class="k">using</span> <span class="nn">System.Web.Http.Dependencies</span><span class="p">;</span>
</span><span class="line"><span class="k">using</span> <span class="nn">Ninject</span><span class="p">;</span>
</span><span class="line"><span class="k">using</span> <span class="nn">Ninject.Syntax</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"><span class="k">namespace</span> <span class="nn">MvcApplication.App_Start</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Provides a Ninject implementation of IDependencyScope</span>
</span><span class="line">   <span class="c1">// which resolves services using the Ninject container.</span>
</span><span class="line">   <span class="k">public</span> <span class="k">class</span> <span class="nc">NinjectDependencyScope</span> <span class="p">:</span> <span class="n">IDependencyScope</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="n">IResolutionRoot</span> <span class="n">resolver</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">NinjectDependencyScope</span><span class="p">(</span><span class="n">IResolutionRoot</span> <span class="n">resolver</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">resolver</span> <span class="p">=</span> <span class="n">resolver</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="kt">object</span> <span class="nf">GetService</span><span class="p">(</span><span class="n">Type</span> <span class="n">serviceType</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">if</span> <span class="p">(</span><span class="n">resolver</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class="line">            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ObjectDisposedException</span><span class="p">(</span><span class="s">&quot;this&quot;</span><span class="p">,</span> <span class="s">&quot;This scope has been disposed&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">         <span class="k">return</span> <span class="n">resolver</span><span class="p">.</span><span class="n">TryGet</span><span class="p">(</span><span class="n">serviceType</span><span class="p">);</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="n">System</span><span class="p">.</span><span class="n">Collections</span><span class="p">.</span><span class="n">Generic</span><span class="p">.</span><span class="n">IEnumerable</span><span class="p">&lt;</span><span class="kt">object</span><span class="p">&gt;</span> <span class="n">GetServices</span><span class="p">(</span><span class="n">Type</span> <span class="n">serviceType</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">if</span> <span class="p">(</span><span class="n">resolver</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class="line">            <span class="k">throw</span> <span class="k">new</span> <span class="nf">ObjectDisposedException</span><span class="p">(</span><span class="s">&quot;this&quot;</span><span class="p">,</span> <span class="s">&quot;This scope has been disposed&quot;</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">         <span class="k">return</span> <span class="n">resolver</span><span class="p">.</span><span class="n">GetAll</span><span class="p">(</span><span class="n">serviceType</span><span class="p">);</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="k">void</span> <span class="nf">Dispose</span><span class="p">()</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="n">IDisposable</span> <span class="n">disposable</span> <span class="p">=</span> <span class="n">resolver</span> <span class="k">as</span> <span class="n">IDisposable</span><span class="p">;</span>
</span><span class="line">         <span class="k">if</span> <span class="p">(</span><span class="n">disposable</span> <span class="p">!=</span> <span class="k">null</span><span class="p">)</span>
</span><span class="line">            <span class="n">disposable</span><span class="p">.</span><span class="n">Dispose</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">         <span class="n">resolver</span> <span class="p">=</span> <span class="k">null</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// This class is the resolver, but it is also the global scope</span>
</span><span class="line">   <span class="c1">// so we derive from NinjectScope.</span>
</span><span class="line">   <span class="k">public</span> <span class="k">class</span> <span class="nc">NinjectDependencyResolver</span> <span class="p">:</span> <span class="n">NinjectDependencyScope</span><span class="p">,</span> <span class="n">IDependencyResolver</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="n">IKernel</span> <span class="n">kernel</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">NinjectDependencyResolver</span><span class="p">(</span><span class="n">IKernel</span> <span class="n">kernel</span><span class="p">)</span> <span class="p">:</span> <span class="k">base</span><span class="p">(</span><span class="n">kernel</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">kernel</span> <span class="p">=</span> <span class="n">kernel</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="n">IDependencyScope</span> <span class="nf">BeginScope</span><span class="p">()</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">return</span> <span class="k">new</span> <span class="nf">NinjectDependencyScope</span><span class="p">(</span><span class="n">kernel</span><span class="p">.</span><span class="n">BeginBlock</span><span class="p">());</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>This file contains two classes. The first, <code>NinjectDependencyScope</code>, provides a
scoping region for dependency resolution. Scopes and Blocks are beyond the
scope (sic) of this post, but the gist is that you may need to provide
different service resolutions for different places in your app.  The second
class is the actual implementation of <code>IDependencyResolver</code>, and since it is
also the <em>global scope</em>, it derives from <code>NinjectDependencyScope</code>. </p>

<h3 id="hooking-it-all-up">Hooking it all up</h3>

<p>Now that we have these classes, we can hook it all up. Returning to the
NinjectWebCommon static class, we only need to add one line to use our new
dependency resolver. We will add this to the <code>CreateKernel</code> method:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>CreateKernel() method</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">private</span> <span class="k">static</span> <span class="n">IKernel</span> <span class="nf">CreateKernel</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="n">var</span> <span class="n">kernel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StandardKernel</span><span class="p">();</span>
</span><span class="line">   <span class="n">kernel</span><span class="p">.</span><span class="n">Bind</span><span class="p">&lt;</span><span class="n">Func</span><span class="p">&lt;</span><span class="n">IKernel</span><span class="p">&gt;&gt;().</span><span class="n">ToMethod</span><span class="p">(</span><span class="n">ctx</span> <span class="p">=&gt;</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">Bootstrapper</span><span class="p">().</span><span class="n">Kernel</span><span class="p">);</span>
</span><span class="line">   <span class="n">kernel</span><span class="p">.</span><span class="n">Bind</span><span class="p">&lt;</span><span class="n">IHttpModule</span><span class="p">&gt;().</span><span class="n">To</span><span class="p">&lt;</span><span class="n">HttpApplicationInitializationHttpModule</span><span class="p">&gt;();</span>
</span><span class="line">
</span><span class="line">   <span class="n">RegisterServices</span><span class="p">(</span><span class="n">kernel</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Install our Ninject-based IDependencyResolver into the Web API config</span>
</span><span class="line">   <span class="n">GlobalConfiguration</span><span class="p">.</span><span class="n">Configuration</span><span class="p">.</span><span class="n">DependencyResolver</span> <span class="p">=</span> <span class="k">new</span> <span class="n">NinjectDependencyResolver</span><span class="p">(</span><span class="n">kernel</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="k">return</span> <span class="n">kernel</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>We simply new up an instance of the <code>NinjectDependencyResolver</code> and assign it
to the <code>DependencyResolver</code> property of the global <code>HttpConfiguration</code> object.</p>

<p>I&rsquo;m still not 100% sure this is the best place to do this, but I tried a few
others and it didn&rsquo;t feel any better. If I later change my mind on this, or if
someone can tell me a better place or way to do this, I will update this post.
In the Unity sample that I link to at the end of this post, they did it in the
Application class. One advantage to doing it here is that we can imagine it
being done for us by a NuGet package (more on that at the end). Choose what
feels right for you.</p>

<h3 id="registering-our-services-with-ninject">Registering our services with Ninject</h3>

<p>The NinjectWebCommon static class already had a nice method called
<code>RegisterServices</code> where I am supposed to express my service bindings, so I
will simply use that. Here is mine:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>RegisterServices method</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">private</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">RegisterServices</span><span class="p">(</span><span class="n">IKernel</span> <span class="n">kernel</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// This is where we tell Ninject how to resolve service requests</span>
</span><span class="line">   <span class="n">kernel</span><span class="p">.</span><span class="n">Bind</span><span class="p">&lt;</span><span class="n">IProductRepository</span><span class="p">&gt;().</span><span class="n">To</span><span class="p">&lt;</span><span class="n">ProductRepository</span><span class="p">&gt;();</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Ninject has a very friendly fluent API for binding instance types to interface
types, so with that one line I am telling it that whenever someone (in this
case ASP.NET) asks for an <code>IProductRepository</code>, it should give back a
<code>ProductRepository</code>. Remember when I said we didn&rsquo;t want these kinds of
dependency resolutions spread all over our code? Now they&rsquo;re not.  We have them
all in one place where they are easy to see and change as needed.</p>

<h2 id="next-steps">Next Steps</h2>

<p>I feel pretty good about the state of this project at this point. In my
previous post I focused on testability and the issues with having a Web API
handler that returned an <code>HttpResponseMessage</code>. In this post I explored how to
resolve the dependencies using Ninject. If this was a real project, I would now
feel pretty good about adding additional controllers, real repository
implementations, etc. I&rsquo;m in a testable state, which I love, and my classes are
loosely coupled while still expressing their dependencies in a clear, readable
way.</p>

<p>I would love to see the <code>NinjectDependencyResolver</code> stuff become a new NuGet
package for others to use, so if someone wants to grab this code and do that,
I&rsquo;m fine with it (and I assume Brad Wilson is too, since this was originally
his code). I don&rsquo;t do enough full-time web development to be a very good
maintainer of such a package, so it really would be better for someone else to
do that instead of me.</p>

<p>I mentioned at the top that you can do this kind of thing with a number of
different dependency injection frameworks. If you want to see an implementation
that uses the <em>patterns &amp; practices</em> Unity container, there is one called
<a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver">Using the Web API Dependency Resolver</a> up on www.asp.net. </p>

<p>If you are looking at the new ASP.NET Web API framework for building out your
new REST APIs, hopefully you&rsquo;ve found these two posts useful. Let me know!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Unit Testing ASP.NET Web API]]></title>
    <link href="http://www.peterprovost.org//blog/2012/06/16/unit-testing-asp-dot-net-web-api/"/>
    <updated>2012-06-16T14:00:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/06/16/unit-testing-asp-dot-net-web-api</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-06-16-unit-testing-asp-dot-net-web-api/ASPNET-Logo.jpg" /></p>

<p>A couple of days ago a colleague pinged me wanting to talk about unit
testing an ASP.NET Web API project. In particular he was having a
hard time testing the POST controller, but it got me thinking I needed
to explore unit testing the new Web API stuff.</p>

<p>Since it is always fun to add unit tests to someone else&rsquo;s codebase, I decided
to start by using the tutorial called <a href="http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations">Creating a Web API that Supports CRUD Operations </a> 
and the <a href="http://code.msdn.microsoft.com/ASP-NET-Web-API-Tutorial-c4761894">provided solution</a> available on www.asp.net. </p>

<!-- more -->

<h2 id="what-should-we-test">What should we test?</h2>

<p>In a Web API project, one of the things you need to ask yourself is, &ldquo;What do
we need to test?&rdquo;</p>

<p>Despite my passion for unit testing and TDD, you might be surprised when I
answer &ldquo;as little as possible.&rdquo; You see, when I&rsquo;m adding tests to legacy code,
I believe strongly that you should only add tests to the things that need it.
There is very little value-add in spending hours adding tests to things that
might not need it.</p>

<p>I tend to follow the <a href="http://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052">WELC approach</a>, focusing on adding tests to either
areas of code that I am about to work on, or areas that I know need some test
coverage. The goal when adding tests for legacy code like this is to &ldquo;pin&rdquo; the
behavior down, so you at least can make positive statements about what it does
do right now. But I only really care about &ldquo;pinning&rdquo; those methods that have
interesting code in them or code we are likely to want to change in the future.
(Many thanks to my friend <a href="http://arlobelshee.com/">Arlo Belshee</a> for promoting the phrase &ldquo;pinning
test&rdquo; for this concept. I really like it.)</p>

<p>So I&rsquo;m not going to bother putting any unit tests on things like
<code>BundleConfig</code>, <code>FilterConfig</code>, or <code>RouteConfig</code>. These classes really just
provide an in-code way of configuring the various conventions in ASP.NET MVC
and Web API.  I&rsquo;m also not going to bother with any of the code in the
<code>Content</code> or <code>Views</code> folders, nor will I unit test any of the JavaScript (but
if this were not just a Web API, but a full web app with important JavaScript,
I would certainly think more about that last one).</p>

<p>Since this is a Web API project, its main purpose is to provide an easy to use
REST JSON API that can be used from apps or web pages. All of the code that
<em>matters</em> is in the <code>Controllers</code> folder, and in particular the
<code>ProductsController</code> class, which is the main API for the project.</p>

<p>This is the class we will unit test today.</p>

<h2 id="unit-tests-not-integration-tests">Unit Tests, not Integration Tests</h2>

<p>Notice that I said <em>unit test</em> in the previous sentence. For me a unit test
is <strong>the smallest bit of code that I can test in isolation from other bits 
of code</strong>. In .NET code, this tends to be classes and methods. Defining unit
test in this way makes it easy to find what to test, but sometimes the <em>how</em>
part can be tough because of the &ldquo;in isolation from other bits&rdquo; part.</p>

<p>When we create tests that bring up large parts of our system, or of the
environment, we are really creating <strong>integration tests</strong>. Don&rsquo;t get me wrong,
I think integration tests are useful and can be important, but I do not
want to get into the habit of depending entirely on integration tests
when writing code. Creating a testable, cohesive, decoupled design is important
to me. It is the only way to achieve the design goal of simplicity (maximizing
the amount of work <strong>not done</strong>).</p>

<p>But in this case we will be adding tests to an existing system. To make the
point, I will try to avoid changing the system if I can. Because of this we may
find ourselves occasionally creating integration tests because we have no
choice.  But we can (and should) use that feedback to think about the design of
what we have and whether it needs some refactoring.</p>

<h2 id="analyzing-the-productscontroller-class">Analyzing the ProductsController class</h2>

<p>The <code>ProductsController</code> class isn&rsquo;t too complex, so it should be pretty easy
to test. Let&rsquo;s take a look at the code we got in the download:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>ProductsController.cs</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
<span class="line-number">40</span>
<span class="line-number">41</span>
<span class="line-number">42</span>
<span class="line-number">43</span>
<span class="line-number">44</span>
<span class="line-number">45</span>
<span class="line-number">46</span>
<span class="line-number">47</span>
<span class="line-number">48</span>
<span class="line-number">49</span>
<span class="line-number">50</span>
<span class="line-number">51</span>
<span class="line-number">52</span>
<span class="line-number">53</span>
<span class="line-number">54</span>
<span class="line-number">55</span>
<span class="line-number">56</span>
<span class="line-number">57</span>
<span class="line-number">58</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">namespace</span> <span class="nn">ProductStore.Controllers</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="k">public</span> <span class="k">class</span> <span class="nc">ProductsController</span> <span class="p">:</span> <span class="n">ApiController</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="k">static</span> <span class="k">readonly</span> <span class="n">IProductRepository</span> <span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductRepository</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">        <span class="k">public</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">Product</span><span class="p">&gt;</span> <span class="n">GetAllProducts</span><span class="p">()</span>
</span><span class="line">        <span class="p">{</span>
</span><span class="line">            <span class="k">return</span> <span class="n">repository</span><span class="p">.</span><span class="n">GetAll</span><span class="p">();</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">        <span class="k">public</span> <span class="n">Product</span> <span class="nf">GetProduct</span><span class="p">(</span><span class="kt">int</span> <span class="n">id</span><span class="p">)</span>
</span><span class="line">        <span class="p">{</span>
</span><span class="line">            <span class="n">Product</span> <span class="n">item</span> <span class="p">=</span> <span class="n">repository</span><span class="p">.</span><span class="n">Get</span><span class="p">(</span><span class="n">id</span><span class="p">);</span>
</span><span class="line">            <span class="k">if</span> <span class="p">(</span><span class="n">item</span> <span class="p">==</span> <span class="k">null</span><span class="p">)</span>
</span><span class="line">            <span class="p">{</span>
</span><span class="line">                <span class="k">throw</span> <span class="k">new</span> <span class="nf">HttpResponseException</span><span class="p">(</span><span class="k">new</span> <span class="n">HttpResponseMessage</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">NotFound</span><span class="p">));</span>
</span><span class="line">            <span class="p">}</span>
</span><span class="line">            <span class="k">return</span> <span class="n">item</span><span class="p">;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">
</span><span class="line">        <span class="k">public</span> <span class="n">IEnumerable</span><span class="p">&lt;</span><span class="n">Product</span><span class="p">&gt;</span> <span class="n">GetProductsByCategory</span><span class="p">(</span><span class="kt">string</span> <span class="n">category</span><span class="p">)</span>
</span><span class="line">        <span class="p">{</span>
</span><span class="line">            <span class="k">return</span> <span class="n">repository</span><span class="p">.</span><span class="n">GetAll</span><span class="p">().</span><span class="n">Where</span><span class="p">(</span>
</span><span class="line">                <span class="n">p</span> <span class="p">=&gt;</span> <span class="kt">string</span><span class="p">.</span><span class="n">Equals</span><span class="p">(</span><span class="n">p</span><span class="p">.</span><span class="n">Category</span><span class="p">,</span> <span class="n">category</span><span class="p">,</span> <span class="n">StringComparison</span><span class="p">.</span><span class="n">OrdinalIgnoreCase</span><span class="p">));</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">
</span><span class="line">        <span class="k">public</span> <span class="n">HttpResponseMessage</span> <span class="nf">PostProduct</span><span class="p">(</span><span class="n">Product</span> <span class="n">item</span><span class="p">)</span>
</span><span class="line">        <span class="p">{</span>
</span><span class="line">            <span class="n">item</span> <span class="p">=</span> <span class="n">repository</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
</span><span class="line">            <span class="n">var</span> <span class="n">response</span> <span class="p">=</span> <span class="n">Request</span><span class="p">.</span><span class="n">CreateResponse</span><span class="p">&lt;</span><span class="n">Product</span><span class="p">&gt;(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">Created</span><span class="p">,</span> <span class="n">item</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">            <span class="kt">string</span> <span class="n">uri</span> <span class="p">=</span> <span class="n">Url</span><span class="p">.</span><span class="n">Link</span><span class="p">(</span><span class="s">&quot;DefaultApi&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="p">{</span> <span class="n">id</span> <span class="p">=</span> <span class="n">item</span><span class="p">.</span><span class="n">Id</span> <span class="p">});</span>
</span><span class="line">            <span class="n">response</span><span class="p">.</span><span class="n">Headers</span><span class="p">.</span><span class="n">Location</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Uri</span><span class="p">(</span><span class="n">uri</span><span class="p">);</span>
</span><span class="line">            <span class="k">return</span> <span class="n">response</span><span class="p">;</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">
</span><span class="line">        <span class="k">public</span> <span class="k">void</span> <span class="nf">PutProduct</span><span class="p">(</span><span class="kt">int</span> <span class="n">id</span><span class="p">,</span> <span class="n">Product</span> <span class="n">contact</span><span class="p">)</span>
</span><span class="line">        <span class="p">{</span>
</span><span class="line">            <span class="n">contact</span><span class="p">.</span><span class="n">Id</span> <span class="p">=</span> <span class="n">id</span><span class="p">;</span>
</span><span class="line">            <span class="k">if</span> <span class="p">(!</span><span class="n">repository</span><span class="p">.</span><span class="n">Update</span><span class="p">(</span><span class="n">contact</span><span class="p">))</span>
</span><span class="line">            <span class="p">{</span>
</span><span class="line">                <span class="k">throw</span> <span class="k">new</span> <span class="nf">HttpResponseException</span><span class="p">(</span><span class="k">new</span> <span class="n">HttpResponseMessage</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">NotFound</span><span class="p">));</span>
</span><span class="line">            <span class="p">}</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">
</span><span class="line">        <span class="k">public</span> <span class="n">HttpResponseMessage</span> <span class="nf">DeleteProduct</span><span class="p">(</span><span class="kt">int</span> <span class="n">id</span><span class="p">)</span>
</span><span class="line">        <span class="p">{</span>
</span><span class="line">            <span class="n">repository</span><span class="p">.</span><span class="n">Remove</span><span class="p">(</span><span class="n">id</span><span class="p">);</span>
</span><span class="line">            <span class="k">return</span> <span class="k">new</span> <span class="nf">HttpResponseMessage</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">NoContent</span><span class="p">);</span>
</span><span class="line">        <span class="p">}</span>
</span><span class="line">
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>We have three Get methods, and a method for each of Post, Put and Delete.</p>

<p>Straight away I see the first problem: The <code>IProductRepository</code> is private and
static. Since I said I didn&rsquo;t want to change the product code, this is an issue.
As a static, readonly, private field, we really don&rsquo;t have any way to replace
it, so in this one case, I will need to change the product to a more testable 
design. This isn&rsquo;t as bad as it looks, however, since in the tutorial they
acknowledge that this is a temporary measure in their code:</p>

<blockquote>
  <p>Calling new ProductRepository() in the controller is not the best design,
because it ties the controller to a particular implementation of
IProductRepository. For a better approach, see <a href="http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver">Using the Web API Dependency Resolver</a>.</p>
</blockquote>

<p>In a future post I will show how to resolve this dependency with something like
Ninject, but for now we will just use manual dependency injection by creating a
testing constructor.  First I will make the repository field non-static. Then I
add a second constructor which allows me to pass in a repository. Finally I
update the default constructor to initialize the field with an instance of the
concrete <code>ProductRepository</code> class. </p>

<p>This approach of creating a testing constructor is a good first step, even if
you are going to later add a dependency injection framework. It allows us to 
provide a stub value for the dependency when we need it, but existing clients
of the class can continue to use the default constructor.</p>

<p>Now the class looks like this.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>ProductsController.cs</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">namespace</span> <span class="nn">ProductStore.Controllers</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">public</span> <span class="k">class</span> <span class="nc">ProductsController</span> <span class="p">:</span> <span class="n">ApiController</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="k">readonly</span> <span class="n">IProductRepository</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">ProductsController</span><span class="p">()</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductRepository</span><span class="p">();</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">ProductsController</span><span class="p">(</span> <span class="n">IProductRepository</span> <span class="n">repository</span> <span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">repository</span> <span class="p">=</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Everything else stays the same</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="testing-the-easy-stuff">Testing the easy stuff</h2>

<p>Now that  we can use the testing constructor to provide a custom instance
of the <code>IProductRepository</code>, we can get back to writing our unit tests.</p>

<p>For these tests I will be using the <a href="http://xunit.codeplex.com/">xUnit.net</a> unit testing framework. I will
also be using Visual Studio 2012 Fakes to provide easy-to-use Stubs for
interfaces we depend on like <code>IProductRepository</code>. After using NuGet to get an
xUnit.net reference in the test project, I added a project reference to the
<code>ProductsStore</code> project. Then by right-clicking on the <code>ProductsStore</code>
reference and choosing <strong>Add Fakes Assembly</strong>, I can create the Stubs I will
use in my tests.</p>

<p>Testing all of the methods except for <code>PostProduct</code> is pretty straightforward.</p>

<h3 id="getallproducts">GetAllProducts</h3>

<p>This is a very simple method that just returns whatever the repository
gives it. No transformations, no deep copies, it just returns the same
<code>IEnumerable</code> it gets from the repository. Here&rsquo;s the test:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Tests for GetAllProducts</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">GetAllProductsReturnsEverythingInRepository</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="c1">// Arrange</span>
</span><span class="line">    <span class="n">var</span> <span class="n">allProducts</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span>
</span><span class="line">                <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span><span class="p">=</span><span class="m">111</span><span class="p">,</span> <span class="n">Name</span><span class="p">=</span><span class="s">&quot;Tomato Soup&quot;</span><span class="p">,</span> <span class="n">Category</span><span class="p">=</span><span class="s">&quot;Food&quot;</span><span class="p">,</span> <span class="n">Price</span> <span class="p">=</span> <span class="m">1.4</span><span class="n">M</span> <span class="p">},</span>
</span><span class="line">                <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span><span class="p">=</span><span class="m">222</span><span class="p">,</span> <span class="n">Name</span><span class="p">=</span><span class="s">&quot;Laptop Computer&quot;</span><span class="p">,</span> <span class="n">Category</span><span class="p">=</span><span class="s">&quot;Electronics&quot;</span><span class="p">,</span> <span class="n">Price</span><span class="p">=</span><span class="m">699.99</span><span class="n">M</span> <span class="p">}</span>
</span><span class="line">            <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">GetAll</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="n">allProducts</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">GetAllProducts</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Same</span><span class="p">(</span><span class="n">allProducts</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h3 id="getproduct">GetProduct</h3>

<p>I used two tests to pin the existing behavior of the <code>GetProduct</code> method. The first
confirms that it returns what the repository gives it, and the second confirms
that it will throw if the repository returns null.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Unit Testing the GetProduct method</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">GetProductReturnsCorrectItemFromRepository</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="c1">// Arrange</span>
</span><span class="line">    <span class="n">var</span> <span class="n">product</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">222</span><span class="p">,</span> <span class="n">Name</span> <span class="p">=</span> <span class="s">&quot;Laptop Computer&quot;</span><span class="p">,</span> <span class="n">Category</span> <span class="p">=</span> <span class="s">&quot;Electronics&quot;</span><span class="p">,</span> <span class="n">Price</span> <span class="p">=</span> <span class="m">699.99</span><span class="n">M</span> <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span> <span class="p">{</span> <span class="n">GetInt32</span> <span class="p">=</span> <span class="n">id</span> <span class="p">=&gt;</span> <span class="n">product</span> <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">GetProduct</span><span class="p">(</span><span class="m">222</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Same</span><span class="p">(</span><span class="n">product</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">GetProductThrowsWhenRepositoryReturnsNull</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">GetInt32</span> <span class="p">=</span> <span class="n">id</span> <span class="p">=&gt;</span> <span class="k">null</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Throws</span><span class="p">&lt;</span><span class="n">HttpResponseException</span><span class="p">&gt;(()</span> <span class="p">=&gt;</span> <span class="n">controller</span><span class="p">.</span><span class="n">GetProduct</span><span class="p">(</span><span class="m">1</span><span class="p">));</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h3 id="getproductsbycategory">GetProductsByCategory</h3>

<p>I just used one test to pin the behavior of <code>GetProductsByCategory</code>.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Unit Testing GetProductsByCategory</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">GetProductsByCategoryFiltersByCategory</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">products</span> <span class="p">=</span> <span class="k">new</span><span class="p">[]</span> <span class="p">{</span>
</span><span class="line">                <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span><span class="p">=</span><span class="m">111</span><span class="p">,</span> <span class="n">Name</span><span class="p">=</span><span class="s">&quot;Tomato Soup&quot;</span><span class="p">,</span> <span class="n">Category</span><span class="p">=</span><span class="s">&quot;Food&quot;</span><span class="p">,</span> <span class="n">Price</span> <span class="p">=</span> <span class="m">1.4</span><span class="n">M</span> <span class="p">},</span>
</span><span class="line">                <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span><span class="p">=</span><span class="m">222</span><span class="p">,</span> <span class="n">Name</span><span class="p">=</span><span class="s">&quot;Laptop Computer&quot;</span><span class="p">,</span> <span class="n">Category</span><span class="p">=</span><span class="s">&quot;Electronics&quot;</span><span class="p">,</span> <span class="n">Price</span><span class="p">=</span><span class="m">699.99</span><span class="n">M</span> <span class="p">}</span>
</span><span class="line">            <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span> <span class="p">{</span> <span class="n">GetAll</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="n">products</span> <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">GetProductsByCategory</span><span class="p">(</span><span class="s">&quot;Electronics&quot;</span><span class="p">).</span><span class="n">ToArray</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Same</span><span class="p">(</span><span class="n">products</span><span class="p">[</span><span class="m">1</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="m">0</span><span class="p">]);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h3 id="putproduct">PutProduct</h3>

<p>I used three tests to pin the various aspects of the <code>PutProduct</code> method:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Unit Testing PutProduct</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
<span class="line-number">40</span>
<span class="line-number">41</span>
<span class="line-number">42</span>
<span class="line-number">43</span>
<span class="line-number">44</span>
<span class="line-number">45</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PutProductUpdatesRepository</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">wasCalled</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">UpdateProduct</span> <span class="p">=</span> <span class="n">prod</span> <span class="p">=&gt;</span> <span class="n">wasCalled</span> <span class="p">=</span> <span class="k">true</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">    <span class="n">var</span> <span class="n">product</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">111</span> <span class="p">};</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Act</span>
</span><span class="line">    <span class="n">controller</span><span class="p">.</span><span class="n">PutProduct</span><span class="p">(</span><span class="m">111</span><span class="p">,</span> <span class="n">product</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="c1">// Assert</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">True</span><span class="p">(</span><span class="n">wasCalled</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PutProductThrowsWhenRepositoryUpdateReturnsFalse</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">UpdateProduct</span> <span class="p">=</span> <span class="n">prod</span> <span class="p">=&gt;</span> <span class="k">false</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Throws</span><span class="p">&lt;</span><span class="n">HttpResponseException</span><span class="p">&gt;(()</span> <span class="p">=&gt;</span> <span class="n">controller</span><span class="p">.</span><span class="n">PutProduct</span><span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="k">new</span> <span class="n">Product</span><span class="p">()));</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PutProductSetsIdBeforeUpdatingRepository</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">updatedId</span> <span class="p">=</span> <span class="n">Int32</span><span class="p">.</span><span class="n">MinValue</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">UpdateProduct</span> <span class="p">=</span> <span class="n">prod</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="n">updatedId</span> <span class="p">=</span> <span class="n">prod</span><span class="p">.</span><span class="n">Id</span><span class="p">;</span> <span class="k">return</span> <span class="k">true</span><span class="p">;</span> <span class="p">}</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">controller</span><span class="p">.</span><span class="n">PutProduct</span><span class="p">(</span><span class="m">123</span><span class="p">,</span> <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">0</span> <span class="p">});</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="m">123</span><span class="p">,</span> <span class="n">updatedId</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h3 id="deleteproduct">DeleteProduct</h3>

<p>Like the PUT handler, we had a few cases to handle to correctly pin the
behavior of this method.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Unit Testing DeleteProduct</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">DeleteProductCallsRepositoryRemove</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">removedId</span> <span class="p">=</span> <span class="n">Int32</span><span class="p">.</span><span class="n">MinValue</span><span class="p">;</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">RemoveInt32</span> <span class="p">=</span> <span class="n">id</span> <span class="p">=&gt;</span> <span class="n">removedId</span> <span class="p">=</span> <span class="n">id</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">controller</span><span class="p">.</span><span class="n">DeleteProduct</span><span class="p">(</span><span class="m">123</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="m">123</span><span class="p">,</span> <span class="n">removedId</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">DeleteProductReturnsResponseMessageWithNoContentStatusCode</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span><span class="p">();</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">DeleteProduct</span><span class="p">(</span><span class="m">123</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">IsType</span><span class="p">&lt;</span><span class="n">HttpResponseMessage</span><span class="p">&gt;(</span><span class="n">result</span><span class="p">);</span>
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">NoContent</span><span class="p">,</span> <span class="n">result</span><span class="p">.</span><span class="n">StatusCode</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="testing-the-harder-stuff-postproduct">Testing the harder stuff: PostProduct</h2>

<p>The <code>PostProduct</code> method is where things get interesting. Because the HTTP spec
says that when you create a resource from a POST you are supposed to return a
<code>Created</code> HTTP status code and include a location to the new resource, the
method we want to test does some funny things to get the HttpResponseMessage
assembled.</p>

<p>My first attempt at a test looked like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Failed attempt at unit testing PostProduct</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PostProductReturnsCreatedStatusCode</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="n">AddProduct</span> <span class="p">=</span> <span class="n">item</span> <span class="p">=&gt;</span> <span class="n">item</span>
</span><span class="line">   <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">PostProduct</span><span class="p">(</span><span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">1</span> <span class="p">});</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">Created</span><span class="p">,</span> <span class="n">result</span><span class="p">.</span><span class="n">StatusCode</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Unfortunately, that didn&rsquo;t work. You end up getting a <code>NullReferenceException</code>
thrown by <code>Request.CreateResponse</code> because it expects a fair amount of web
config stuff to have been assembled. This is a bummer, but it is what it is.</p>

<p>I reached out to <a href="http://bradwilson.typepad.com/">Brad Wilson</a> for help, and we figured out how to test this
without going all the way to creating a web server/client pair, but there is
clearly a lot of extra non-test code still running. We had to assemble a whole
bunch of interesting configuration and routing classes to make the
<code>Request.CreateResponse</code> method happy, but it did work.</p>

<p>The first test we wrote looked like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Successfully unit testing PostProduct</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PostProductReturnsCreatedStatusCode</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="n">AddProduct</span> <span class="p">=</span> <span class="n">item</span> <span class="p">=&gt;</span> <span class="n">item</span>
</span><span class="line">   <span class="p">};</span>
</span><span class="line">
</span><span class="line">   <span class="n">var</span> <span class="n">config</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpConfiguration</span><span class="p">();</span>
</span><span class="line">   <span class="n">var</span> <span class="n">request</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpRequestMessage</span><span class="p">(</span><span class="n">HttpMethod</span><span class="p">.</span><span class="n">Post</span><span class="p">,</span> <span class="s">&quot;http://localhost/api/products&quot;</span><span class="p">);</span>
</span><span class="line">   <span class="n">var</span> <span class="n">route</span> <span class="p">=</span> <span class="n">config</span><span class="p">.</span><span class="n">Routes</span><span class="p">.</span><span class="n">MapHttpRoute</span><span class="p">(</span><span class="s">&quot;DefaultApi&quot;</span><span class="p">,</span> <span class="s">&quot;api/{controller}/{id}&quot;</span><span class="p">);</span>
</span><span class="line">   <span class="n">var</span> <span class="n">routeData</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpRouteData</span><span class="p">(</span><span class="n">route</span><span class="p">,</span> <span class="k">new</span> <span class="n">HttpRouteValueDictionary</span> <span class="p">{</span> <span class="p">{</span> <span class="s">&quot;controller&quot;</span><span class="p">,</span> <span class="s">&quot;products&quot;</span> <span class="p">}</span> <span class="p">});</span>
</span><span class="line">   <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">   <span class="n">controller</span><span class="p">.</span><span class="n">ControllerContext</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpControllerContext</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">routeData</span><span class="p">,</span> <span class="n">request</span><span class="p">);</span>
</span><span class="line">   <span class="n">controller</span><span class="p">.</span><span class="n">Request</span> <span class="p">=</span> <span class="n">request</span><span class="p">;</span>
</span><span class="line">   <span class="n">controller</span><span class="p">.</span><span class="n">Request</span><span class="p">.</span><span class="n">Properties</span><span class="p">[</span><span class="n">HttpPropertyKeys</span><span class="p">.</span><span class="n">HttpConfigurationKey</span><span class="p">]</span> <span class="p">=</span> <span class="n">config</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">PostProduct</span><span class="p">(</span><span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">1</span> <span class="p">});</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">Created</span><span class="p">,</span> <span class="n">result</span><span class="p">.</span><span class="n">StatusCode</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>In a future post, I may take a look at how we might use Visual Studio 2010
Fakes to create a Shim to remove all that config stuff, but this will have to
do for now.</p>

<p>Since I knew I needed to make a few more tests to adequately pin the behavior
of <code>PostProduct</code>, I refactored out the ugly config code into a private method
in the test class called <code>SetupControllerForTests</code>. I find that when I have
issues like this, I really like to keep the weird setup code close to the
tests.  I generally prefer this over creating abstract test classes because I
like it to be very obvious what is happening. I also like my tests to be easily
read and understood without having to jump around in the class hierarchy.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Helper method for configuring the controller</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">private</span> <span class="k">static</span> <span class="k">void</span> <span class="nf">SetupControllerForTests</span><span class="p">(</span><span class="n">ApiController</span> <span class="n">controller</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">config</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpConfiguration</span><span class="p">();</span>
</span><span class="line">    <span class="n">var</span> <span class="n">request</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpRequestMessage</span><span class="p">(</span><span class="n">HttpMethod</span><span class="p">.</span><span class="n">Post</span><span class="p">,</span> <span class="s">&quot;http://localhost/api/products&quot;</span><span class="p">);</span>
</span><span class="line">    <span class="n">var</span> <span class="n">route</span> <span class="p">=</span> <span class="n">config</span><span class="p">.</span><span class="n">Routes</span><span class="p">.</span><span class="n">MapHttpRoute</span><span class="p">(</span><span class="s">&quot;DefaultApi&quot;</span><span class="p">,</span> <span class="s">&quot;api/{controller}/{id}&quot;</span><span class="p">);</span>
</span><span class="line">    <span class="n">var</span> <span class="n">routeData</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpRouteData</span><span class="p">(</span><span class="n">route</span><span class="p">,</span> <span class="k">new</span> <span class="n">HttpRouteValueDictionary</span> <span class="p">{</span> <span class="p">{</span> <span class="s">&quot;controller&quot;</span><span class="p">,</span> <span class="s">&quot;products&quot;</span> <span class="p">}</span> <span class="p">});</span>
</span><span class="line">
</span><span class="line">    <span class="n">controller</span><span class="p">.</span><span class="n">ControllerContext</span> <span class="p">=</span> <span class="k">new</span> <span class="n">HttpControllerContext</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">routeData</span><span class="p">,</span> <span class="n">request</span><span class="p">);</span>
</span><span class="line">    <span class="n">controller</span><span class="p">.</span><span class="n">Request</span> <span class="p">=</span> <span class="n">request</span><span class="p">;</span>
</span><span class="line">    <span class="n">controller</span><span class="p">.</span><span class="n">Request</span><span class="p">.</span><span class="n">Properties</span><span class="p">[</span><span class="n">HttpPropertyKeys</span><span class="p">.</span><span class="n">HttpConfigurationKey</span><span class="p">]</span> <span class="p">=</span> <span class="n">config</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Now that I have the helper method, I can refactor the status code test, and
add two more to check the location and to confirm that it actually calls the 
<code>AddProduct</code> method on the repository.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Final unit tests for PostProduct</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
<span class="line-number">40</span>
<span class="line-number">41</span>
<span class="line-number">42</span>
<span class="line-number">43</span>
<span class="line-number">44</span>
<span class="line-number">45</span>
<span class="line-number">46</span>
<span class="line-number">47</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PostProductReturnsCreatedStatusCode</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">AddProduct</span> <span class="p">=</span> <span class="n">item</span> <span class="p">=&gt;</span> <span class="n">item</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">    <span class="n">SetupControllerForTests</span><span class="p">(</span><span class="n">controller</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">PostProduct</span><span class="p">(</span><span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">1</span> <span class="p">});</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">HttpStatusCode</span><span class="p">.</span><span class="n">Created</span><span class="p">,</span> <span class="n">result</span><span class="p">.</span><span class="n">StatusCode</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line">
</span><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PostProductReturnsTheCorrectLocationInResponseMessage</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">AddProduct</span> <span class="p">=</span> <span class="n">item</span> <span class="p">=&gt;</span> <span class="n">item</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">    <span class="n">SetupControllerForTests</span><span class="p">(</span><span class="n">controller</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">PostProduct</span><span class="p">(</span><span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">111</span> <span class="p">});</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="s">&quot;http://localhost/api/products/111&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">.</span><span class="n">Headers</span><span class="p">.</span><span class="n">Location</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
</span><span class="line"><span class="p">}</span>
</span><span class="line">
</span><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">PostProductCallsAddOnRepositoryWithProvidedProduct</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="n">var</span> <span class="n">providedProduct</span> <span class="p">=</span> <span class="k">default</span><span class="p">(</span><span class="n">Product</span><span class="p">);</span>
</span><span class="line">    <span class="n">var</span> <span class="n">repo</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIProductRepository</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">AddProduct</span> <span class="p">=</span> <span class="n">item</span> <span class="p">=&gt;</span> <span class="n">providedProduct</span> <span class="p">=</span> <span class="n">item</span>
</span><span class="line">    <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">controller</span> <span class="p">=</span> <span class="k">new</span> <span class="n">ProductsController</span><span class="p">(</span><span class="n">repo</span><span class="p">);</span>
</span><span class="line">    <span class="n">SetupControllerForTests</span><span class="p">(</span><span class="n">controller</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">var</span> <span class="n">product</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Product</span> <span class="p">{</span> <span class="n">Id</span> <span class="p">=</span> <span class="m">111</span> <span class="p">};</span>
</span><span class="line">    <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">controller</span><span class="p">.</span><span class="n">PostProduct</span><span class="p">(</span><span class="n">product</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">    <span class="n">Assert</span><span class="p">.</span><span class="n">Same</span><span class="p">(</span><span class="n">product</span><span class="p">,</span> <span class="n">providedProduct</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<h2 id="conclusion">Conclusion</h2>

<p>And now we&rsquo;re done. We have successfully &ldquo;pinned&rdquo; the behavior of the entire
<code>ProductsController</code> class so if we later need to refactor it, we have a way of
knowing what the current behavior is. As I discussed in 
<a href="http://peterprovost.org/blog/2012/04/25/visual-studio-11-fakes-part-2/">my previous post about VS 2012 Shims</a>, we can&rsquo;t refactor without being able
to confirm the current behavior, otherwise we will end up in a Catch-22.
Creating &ldquo;pinning&rdquo; or &ldquo;characterization&rdquo; tests like those created here are the
first step to being able to safely and confidently refactor or add new
behaviors.</p>

<p>Hopefully this post showed you a few new things. First, we got to see another
example of using Stubs in unit tests. Also, we learned a bit about how to deal
with the nastiness around the <code>HttpRequestMessage.CreateResponse</code> extension
method.</p>

<p>Personally I wish that POST handler was as easy to test as the rest of the
controller was. One of my favorite things about MVC was always that the
separation of concerns let me test things in isolation. When a controller
doesn&rsquo;t have tight dependencies on the web stack, it is a well-behaved
controller. Unfortunately, when you want to create a controller that followed
the HTTP spec for POST, you will find this a bit hard today. But at least we
found a way around it.</p>

<p>Let me know what you think!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Anatomy of a File-based Unit Test Plugin]]></title>
    <link href="http://www.peterprovost.org//blog/2012/06/15/anatomy-of-a-file-based-unit-test-plugin/"/>
    <updated>2012-06-15T10:43:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/06/15/anatomy-of-a-file-based-unit-test-plugin</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-06-15-anatomy-of-a-file-based-unit-test-plugin/Matthews-Blog-Post.png" /></p>

<p>I&rsquo;ve been working on a series of posts about authoring a new unit test plugin
for Visual Studio 2012, but today my friend Matthew Manela, author of the
Chutzpah test plugin, sent me <a href="http://matthewmanela.com/blog/anatomy-of-the-chutzpah-test-adapter-for-vs-2012-rc/">a post he did a few days ago</a> that discusses
the main interfaces he had to use to make his plugin.</p>

<p>The Chutzpah plugin runs JavaScript unit tests that are written in either the
QUnit or Jasmine test frameworks. Since JavaScript files don&rsquo;t get compiled
into a DLL or EXE, he had to create custom implementations of what we call a
<strong>test container</strong>.</p>

<!-- more -->

<p>A test container represents a thing that contains tests. For .NET and C++ unit
testing, this is a DLL or EXE, and Visual Studio 2012 comes with a built-in
test container subsystem for them. But when you need to support tests contained
in other files, e.g. JavaScript files, then you need to do a bit more.</p>

<p>Matthews post does a great job of going through each of these interfaces,
discussing what each is for and what he did for it in his plugin.</p>

<blockquote><p>The Chutzpah test adapter revolves around four interfaces:</p><p>1. ITestContainer – Represents a file that contains tests<br />2. ITestContainerDiscoverer – Finds all files that contain tests<br />3. ITestDiscoverer – Finds all tests within a test container<br />4. ITestExecutor – Runs the tests found inside the test container</p><footer><strong>Matthew Manela</strong> <cite><a href="http://matthewmanela.com/blog/anatomy-of-the-chutzpah-test-adapter-for-vs-2012-rc/">matthewmanela.com/blog/&hellip;</a></cite></footer></blockquote>

<p>You can read the entire post here:<br />
<a href="http://matthewmanela.com/blog/anatomy-of-the-chutzpah-test-adapter-for-vs-2012-rc/">http://matthewmanela.com/blog/anatomy-of-the-chutzpah-test-adapter-for-vs-2012-rc/</a></p>

<p>And if you want to see the source for his plugin, you can read it all here
(look for the VS11.Plugin folder on the left side):<br />
<a href="http://chutzpah.codeplex.com/SourceControl/BrowseLatest">http://chutzpah.codeplex.com/SourceControl/BrowseLatest</a></p>

<p>Nice post Matthew! Thanks for providing it to the community.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My VS2012 Wallpapers]]></title>
    <link href="http://www.peterprovost.org//blog/2012/06/05/my-vs2012-wallpapers/"/>
    <updated>2012-06-05T12:44:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/06/05/my-vs2012-wallpapers</id>
    <content type="html"><![CDATA[<p>I&rsquo;m getting ready for TechEd 2012 this month and realized I wanted to have a
nice desktop wallpaper for my laptop. Well, I&rsquo;d not seen any popping up on
twitter or blogs yet, so I grabbed my trusty <a href="http://gimp-win.sourceforge.net/stable.html">GIMP image editor</a> and some
base images of the new VS 2012 logo and got to work.</p>

<p>Here are a few of my favorites, but I&rsquo;ve not decided which I will use on my
laptop.</p>

<!-- more -->

<iframe src="https://skydrive.live.com/embed?cid=67749B74E5D97F43&amp;resid=67749B74E5D97F43%211062&amp;authkey=AOtTkQepxfXvXgA" width="320" height="200" frameborder="0" scrolling="no"></iframe>
<iframe src="https://skydrive.live.com/embed?cid=67749B74E5D97F43&amp;resid=67749B74E5D97F43%211066&amp;authkey=ABYmwCqUppSG220" width="320" height="200" frameborder="0" scrolling="no"></iframe>
<iframe src="https://skydrive.live.com/embed?cid=67749B74E5D97F43&amp;resid=67749B74E5D97F43%211054&amp;authkey=ALCSzzwpkZGFdc0" width="320" height="200" frameborder="0" scrolling="no"></iframe>

<p style="padding-top: 1em">You can see/download them all by clicking on one of the links above or you
browse them on <a href="http://sdrv.ms/KatLcZ">my SkyDrive</a>.  They are all 1680x1050 PNG files and have
been <a href="http://www.hanselman.com/blog/AddingPNGOUTToTheExplorerRightClickContextMenu.aspx">compressed with PNGOUT</a> to be as small as possible. I may end up
making more so keep an eye on that folder if you&rsquo;re interested.</p>

<p>If you find them useful, please let me know.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Speaking Schedule - 2012]]></title>
    <link href="http://www.peterprovost.org//blog/2012/06/01/my-speaking-schedule-2012/"/>
    <updated>2012-06-01T11:19:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/06/01/my-speaking-schedule-2012</id>
    <content type="html"><![CDATA[<p>I tweeted a bit about the craziness coming up for me in June, but I realized
today that I&rsquo;ve not posted the whole schedule. So if you&rsquo;re interested and
wondering what I&rsquo;ll be talking about, here&rsquo;s the complete list (in chonological
order).</p>

<p>The first two are now behind me, but the twin TechEds are coming up fast. Also,
I&rsquo;ve got a couple that are currently &ldquo;maybes&rdquo; so once they are confirmed I&rsquo;ll
update this post. If you want to contact me about speaking at an event, please
use my about.me contact page.</p>

<!-- more -->

<p><strong>Update 2012-06-06</strong> - I had to pull out of Agile.NET Columbus due to a
conference date change that wasn&rsquo;t compatible with my schedule. Bummer.</p>

<h2 id="microsoft-techready-14-img-classright-srcimagesblog2012-06-01-my-speaking-schedule-2012tr14png-titletechready-14-">Microsoft TechReady 14 <img class="right" src="http://www.peterprovost.org//images/blog/2012-06-01-my-speaking-schedule-2012/tr14.png" title="TechReady 14" /></h2>
<p><em>January 30 - February 3, 2012 &ndash; Seattle, WA</em></p>

<ul>
  <li><strong>Introducing the New VS11 Unit Testing Experience</strong> - Ramping up the field
on where we&rsquo;re taking the Unit Test experience in VS11 and beyond.</li>
</ul>

<h2 id="microsoft-mvp-summit-img-classright-srcimagesblog2012-06-01-my-speaking-schedule-2012microsoftmvplogopng-titlemvp-global-summit-">Microsoft MVP Summit <img class="right" src="http://www.peterprovost.org//images/blog/2012-06-01-my-speaking-schedule-2012/Microsoft_MVP_logo.png" title="MVP Global Summit" /></h2>
<p><em>February 28 - March 2, 2012 &ndash; Bellevue, WA</em></p>

<ul>
  <li><strong>Agile Development in VS11 - Unit Testing, Fakes and Code Clone</strong> - Bringing
 the MVPs up to speed on the new stuff in VS11 Beta.</li>
</ul>

<h2 id="teched-north-america-2012-img-classright-srcimagesblog2012-06-01-my-speaking-schedule-2012teched-na-2012png-titleteched-north-america-2012-">TechEd North America 2012 <img class="right" src="http://www.peterprovost.org//images/blog/2012-06-01-my-speaking-schedule-2012/TechEd-NA-2012.png" title="TechEd North America 2012" /></h2>
<p><em>June 11-14, 2012 &ndash; Orlando, FL</em></p>

<ul>
  <li><strong>DEV214 - Introducing the New Visual Studio 11 Unit Testing Experience</strong> -
An updated version of the talk I&rsquo;d previously given for internal and MVP
audiences based on current bits and plans.</li>
  <li><strong>AAP401 - Real World Developer Testing with Visual Studio 11</strong> - David Starr
and I will go through a bunch of real-world unit test scenarios
sharing our tips and tricks for getting them under test while ensuring they
are still good unit tests.</li>
  <li><strong>DEV411 - Testing Un-testable Code with Fakes in Visual Studio 11</strong> - A deep
dive into VS 2012 Fakes, focusing on things that are either hard to unit test
or that you might think aren&rsquo;t unit testable at all.</li>
  <li><strong>DEV318 - Working on an Agile Team with Visual Studio 11 and Team Foundation
Server 11</strong> - Gregg Boer and I will take you through the lifecycle of an agile
team using all the great new features in VS 2012 and TFS 2012.</li>
</ul>

<h2 id="teched-europe-2012-img-classright-srcimagesblog2012-06-01-my-speaking-schedule-2012teched-eu-2012png-titleteched-europe-2012-">TechEd Europe 2012 <img class="right" src="http://www.peterprovost.org//images/blog/2012-06-01-my-speaking-schedule-2012/TechEd-EU-2012.png" title="TechEd Europe 2012" /></h2>
<p><em>June 26-29, 2012 &ndash; Amsterdam, RAI</em></p>

<ul>
  <li><em>Same as TechEd North America 2012</em></li>
</ul>

<h2 id="denver-visual-studio-user-group-img-classright-srcimagesblog2012-06-01-my-speaking-schedule-2012vs2012png-titlevisual-studio-2012-">Denver Visual Studio User Group <img class="right" src="http://www.peterprovost.org//images/blog/2012-06-01-my-speaking-schedule-2012/vs2012.png" title="Visual Studio 2012" /></h2>
<p><em>July 23, 2012 &ndash; Denver, CO</em></p>

<ul>
  <li><strong>Unit Testing and TDD with Visual Studio 2012</strong> - In Visual Studio 11, a lot
of changes have happened to make unit testing for flexible and powerful. For
agile and non-agile teams who are writing unit tests, these changes will let
you work faster and stay focused on your code. In this session Peter Provost,
long time TDD advocate and Senior Program Manager Lead in Visual Studio ALM
Tools and designer of this new experience, will take us through the
experience, focusing on the what the biggest differences are and why they are
important to developers.</li>
</ul>

<h2 id="agile-2012-img-classright-srcimagesblog2012-06-01-my-speaking-schedule-2012agile2012png-titleagile-2012-">Agile 2012 <img class="right" src="http://www.peterprovost.org//images/blog/2012-06-01-my-speaking-schedule-2012/Agile2012.png" title="Agile 2012" /></h2>
<p><em>August 13-17, 2012 &ndash; Dallas, TX</em></p>

<ul>
  <li><strong>Talk Title TBD</strong></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Take on Unit Testing Private Methods]]></title>
    <link href="http://www.peterprovost.org//blog/2012/05/31/my-take-on-unit-testing-private-methods/"/>
    <updated>2012-05-31T00:29:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/05/31/my-take-on-unit-testing-private-methods</id>
    <content type="html"><![CDATA[<h2 id="the-simple-answer-just-say-notrade">The simple answer: Just Say No&trade;</h2>

<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-31-my-take-on-unit-testing-private-methods/Rodin-The-Thinker-180x240.jpg" title="The Thinker" /></p>

<p>Every now and then I get an email, see a forum post, or get a query at a
conference about this topic. It typically goes like this:</p>

<p><em>&ldquo;When you&rsquo;re doing TDD, how do you test your private methods?&rdquo;</em></p>

<p>The answer of course, is simple: <strong>You don&rsquo;t. And you shouldn&rsquo;t.</strong></p>

<p>This has been written up in numerous places, by many people smarter than I, but
apparently there are still people who don&rsquo;t understand our point.</p>

<!-- more -->

<h2 id="private-methods-and-tdd">Private Methods and TDD</h2>

<p>I do create private methods while doing TDD, as a result of aggressive
refactoring. But from the perspective of the tests I&rsquo;m writing, these private
methods are entirely an implementation detail. They are, and should remain,
irrelevant to the tests.  As many people have pointed out over the years,
putting tests on internals of any kind inhibits free refactoring. </p>

<p>They are internals. They are private. Keep them that way.</p>

<p><img class="left" src="http://www.peterprovost.org//images/blog/2012-05-02-the-only-way-to-learn-tdd-kata/Red-Green-Refactor-Bunny.png" /></p>

<p>What I like to do every now and then, when I&rsquo;m in the green phase of the TDD
cycle, is stop and look at the private methods I&rsquo;ve extracted in a class.  If I
see a bunch of them, it makes me stop and ask, &ldquo;What is this code trying to
tell me?&rdquo; </p>

<p>More often than not, it is telling me there is another class needed.  I look at
the method parameters. I look at the fields they reference. I look for a
pattern.  Often, I will find that a whole new class is lurking in there.</p>

<p>So I refactor it out. And my old tests should STILL pass.</p>

<p>Now it is time to go back and add some new tests for this new class. Or if you
feel like being really rigorous, delete that class and write it from scratch,
using TDD. (But I admit, I don&rsquo;t really ever do that except for practice when
doing longer kata.)</p>

<p>At the risk of repeating myself, I&rsquo;ll say it one more time: <strong>Just Say
No&trade;</strong></p>

<h2 id="what-about-the-public-api">What about the public API?</h2>

<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-31-my-take-on-unit-testing-private-methods/Book.jpg" title="An old book" /></p>

<p>I most often hear this from people who&rsquo;ve grown up creating frameworks and
APIs. The essence of this argument is rooted in the idea that making something
have private accessibility (a language construct) somehow reduces the supported
public API of your product.</p>

<p>I would argue that this is a red herring, or the fallacy of irrelevant
conclusion.  The issue of &ldquo;supported public API&rdquo; and &ldquo;public method visibility&rdquo;
are unfortunately related only by the use of the word <strong>public</strong>. </p>

<p>Making a class or method public does not make it a part of your public API. You
can mark is as &ldquo;for internal use only&rdquo; with various attributes or code
constructs (depending on your language). You can put it into an &ldquo;internals
only&rdquo; package or namespace. You can simply document it in your docs as being
&ldquo;internal use only&rdquo;.  </p>

<p>All of these are at least as good, if not better, than making it private or
internal. Why? Because it frees the developers to use good object-oriented
design, to refactor aggressively and to TDD/unit test effectively. It makes
your code more flexible and the developers are more free to do what they are
asked to do.</p>

<h2 id="but-customers-will-use-it-anyway">But customers will use it anyway!</h2>

<p>Consider the answer to these questions:</p>

<ol>
  <li>You have an API that has public accessibility, and it is marked <em>Internal use
   only</em> in any of the ways I mentioned above. A customer calls you up and says
   &ldquo;When I use this API it doesn&rsquo;t work as expected.&rdquo; What is your response?</li>
  <li>You have an API that has public accessibility, and it is marked <em>Internal use
   only</em> in any of the ways I mentioned above. You changed the API between
   product versions. The customer complains that the API changed. What is your
   response?</li>
</ol>

<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-31-my-take-on-unit-testing-private-methods/Just_Say_No.jpg" title="Just Say No" /></p>

<p>In each case, I would argue that the answer is the same. You simply say, &ldquo;That
API is not for public consumption. It is for internal use only, which is why it
was marked &amp; documented as such. Do not use it. Go away.&rdquo;</p>

<p>If you feel that you wouldn&rsquo;t do that. Consider these slightly revised versions
of the same questions:</p>

<ol>
  <li>You have an API that has private accessibility. A customer calls you up and
   says, &ldquo;When I used private reflection to call this API, it didn&rsquo;t work as
   expected.&rdquo; What is your response?</li>
  <li>You have an API that has private accessibility. You changed the API between
   product versions. The customer complains that the API changed. What is your
   response?</li>
</ol>

<p>If you are working in a language like C# (or Java or Ruby or any other that
supports reflection), you know that making a method, property or field have
private accessibility does not ever prevent a user from calling it. So don&rsquo;t
delude yourself into thinking that it will prevent them from using it. Trust me
on this, I&rsquo;ve been doing &ldquo;privates hacking&rdquo; since the good old days of C++ and
pointer offset math. </p>

<h2 id="other-arguments">Other arguments</h2>

<p>If you think it is about security, that argument doesn&rsquo;t hold water either. If
you spend just a little bit of time researching how hackers create aimbots and
other cheats for popular online games, you will see that even in C++, making
things private doesn&rsquo;t protect you. A little pointer arithmetic and structure
mapping, and you can do whatever you want.</p>

<p><img class="left" src="http://www.peterprovost.org//images/blog/2012-05-31-my-take-on-unit-testing-private-methods/179px-Aristotle_Altemps_Inv8575.jpg" title="Artistotle" /></p>

<p>A very interesting argument I&rsquo;ve heard on this one had to do with IntelliTrace
in Visual Studio. It went something like this: &ldquo;When you make it public, it
will show up in the statement completion drop-down, and we don&rsquo;t want to confuse
people with all that noise.&rdquo;</p>

<p>When I pointed out that with some simple refactoring (e.g. use a sub-namespace
called Internal and actually do the Extract Class refactoring I mentioned
above), they realized that they were designing for the wrong thing.</p>

<p>A final one I get at about this point goes something like this: &ldquo;Information
hiding and encapsulation are fundamental to good object-oriented design.&rdquo; Let
me say that I believe in information hiding. I think fields should be private.
And I think external actors should only manipulate them through public methods.
If you look at languages like Smalltalk and Ruby, this is just the way the
language works.  (Yes, yes, I know&hellip; <code>private</code> in Ruby means something
different. But the point is still the same.)</p>

<h2 id="conclusion">Conclusion</h2>

<p>You can make a well-factored design that doesn&rsquo;t require private methods.  You
can design an easy to understand API that is usable and not too noisy by
applying well-targeted refactorings and design patterns. You can remove the
support burden by clearly documenting what is meant to be used and what is not.
And it doesn&rsquo;t help with security.</p>

<p>Focus on what matters. Let the tests drive the code and let the code tell you
when it needs to be refactored. You don&rsquo;t need to make everything private.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Updated NUnit Plugin for VS11 Released]]></title>
    <link href="http://www.peterprovost.org//blog/2012/05/04/updated-nunit-plugin-for-vs11-released/"/>
    <updated>2012-05-04T11:07:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/05/04/updated-nunit-plugin-for-vs11-released</id>
    <content type="html"><![CDATA[<p>Good news!! Last night I got an email from Charlie Poole, the maintainer of
NUnit pointing me to a blog post he&rsquo;d just made:</p>

<blockquote><p>Today, I’m releasing version 0.92 of the NUnit Adapter for Visual Studio and<br />creating a new project under the NUnit umbrella to hold it’s source code, track<br />bugs, etc.<br /> <br />In case you didn’t know, Visual Studio 11 has a unit test window, which<br />supports running tests using any test framework for which an adapter has been<br />installed. The NUnit adapter has been available on Code Gallery since the<br />initial VS Developer preview. This release fixes some problems with running<br />tests that target .NET 2.0/3.5 and with debugging through tests in the unit<br />test window.</p><footer><strong>Charlie Poole</strong> <cite><a href="http://nunit.net/blogs/?p=123">nunit.net/blogs/?p=123/&hellip;</a></cite></footer></blockquote>

<!-- more -->

<p>This is great news because if you&rsquo;ve tried to use NUnit with VS11 Beta before
now, you probably noticed that you couldn&rsquo;t actually run or debug a selected
test or tests. When you tried, you ended up either getting a failed run
or having all tests run. Clearly not good.</p>

<p>The fix was pretty simple, and I want to thank our team for helping find the
issue and also of course thank Charlie for getting it out to customers to 
get them unblocked while using NUnit.</p>

<p>He&rsquo;s also pushed some new content about the plugin:</p>

<ul>
  <li><a href="http://launchpad.net/nunit-vs-adapter">NUnit Test Adapter Project Page</a></li>
  <li><a href="http://bugs.launchpad.net/nunit-vs-adapter">Bug Tracker</a></li>
  <li><a href="http://nunit.org/index.php?p=vsTestAdapter&amp;r=2.6">Documentation</a></li>
  <li><a href="http://visualstudiogallery.msdn.microsoft.com/6ab922d0-21c0-4f06-ab5f-4ecd1fe7175d">Updated the VS Gallery page</a></li>
</ul>

<p>So if you are experimenting with VS11 and are an NUnit user, be sure to get
this update.</p>

<p>And of course, keep the feedback on VS11 Unit Testing coming!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Games I'm Enjoying Lately]]></title>
    <link href="http://www.peterprovost.org//blog/2012/05/03/games-im-enjoying-lately/"/>
    <updated>2012-05-03T20:24:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/05/03/games-im-enjoying-lately</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-03-games-im-enjoying-lately/sniper.jpg" /></p>

<p>I first started playing games on computers the day after we got our first
computer way back in 1978 or 1979 on our venerable <a href="http://en.wikipedia.org/wiki/Ohio_Scientific">Ohio Scientific Challenger 4P</a>. 
Man, those were good times. Tanks and mazes&hellip; that was about all we had.</p>

<p>In the 80s, when we lived in Egypt and had an Apple II+, I would type the games
in, line-by-line, from the backs of magazines. I&rsquo;m still convinced that I really
learned to debug computer programs back in these days.</p>

<p>And I&rsquo;ve been a gamer ever since.</p>

<p>Lately, I&rsquo;ve been playing a bunch of different games, looking for something
that I can really get into for a while. Although I&rsquo;ve had fun playing almost
all of them, I&rsquo;ve still not found any with real staying power for me. But I
have had a chance to play a number of different games on a few different 
platforms.</p>

<p>Here are some review of what I&rsquo;ve been playing lately, and a look to what I&rsquo;m
waiting for.</p>

<!-- more -->

<h2 id="what-ive-been-playing">What I&rsquo;ve Been Playing</h2>

<h3 id="world-of-warcraft---pc">World of Warcraft - PC</h3>

<p><strong>Metacritic Score: 90-93</strong></p>

<p><a href="http://www.amazon.com/gp/product/B002I0HKIU/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B002I0HKIU"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B002I0HKIU&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>Yes, I still play Wow. I&rsquo;ve been playing since the tail-end of so-called
&ldquo;Vanilla Wow&rdquo;, but I have severely cut back on the amount of time I play it. I
still raid two nights a week with my long time guildmates and friends but on
non-raiding nights, I just can&rsquo;t seem to bring myself to login and play.</p>

<p>The thing I always liked about Wow was the team &amp; social nature of the game.
Working together with 5, 10 or 25 people (or even 40 in the olden days) to
figure out how to defeat one of the dungeons is a blast. Problem solving,
leadership, joking, laughing and then the joy of victory. But while the
non-raiding parts of the game used to keep me engaged, it has lost its luster,
which is why I&rsquo;ve been dabbling in so many others.</p>

<h3 id="dead-space-2---pc">Dead Space 2 - PC</h3>

<p><strong>Metacritic Score: 87</strong></p>

<p><a href="http://www.amazon.com/gp/product/B002I0HJ68/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B002I0HJ68"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B002I0HJ68&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>This game can be pretty scary at times, as you expect from a survival horror
FPS. Tons of scary shit coming at you all the time. The upgrade and leveling
system was a bit weird, but I muddled my way through and seemed to maintain
decent killing power.</p>

<p>The magnetic grip things is a blast to kill with. You can grab steel rods, saw
blades, etc. (kinda like Half-Life) and rip the monsters in half. Great fun and
recommended if you like the scary stuff.</p>

<p>Like so many FPS/RPG games though, I didn&rsquo;t actually finish it before moving on
to something else.</p>

<h3 id="skyrim---pc">Skyrim - PC</h3>

<p><strong>Metacritic Score: 94</strong></p>

<p><a href="http://www.amazon.com/gp/product/B004HYIAPM/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B004HYIAPM"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B004HYIAPM&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>The whole world was raving about Skyrim a few months ago, so I had to give it a
try. I got it for a steal during one of Steam&rsquo;s special sales, and I have to
admit it is a fun game. When the modding community really got rolling that was
fun too.</p>

<p>But I have a problem in general with single player RPGs, and it is that I just
start to get bored. I don&rsquo;t really &ldquo;role play&rdquo;, so for me the fun is in the
puzzles and challenges. In every single player RPG I&rsquo;ve played I find that once
I figure out the basic fighting mechanics, it becomes very rote, very quickly.</p>

<p>I will say, this game is one of the most beautiful game worlds I&rsquo;ve played in a
long time. If you install the HD textured pack (free downloadable content) it
is even better, but make sure you have a good graphics card.</p>

<p>I still may return to Skyrim for some more dragon killing and whatnot, but it
just hasn&rsquo;t been able to draw me back in more than a month.</p>

<h3 id="tom-clancys-splinter-cell-conviction---pc">Tom Clancy&rsquo;s Splinter Cell: Conviction - PC</h3>

<p><strong>Metacritic Score: 83</strong></p>

<p><a href="http://www.amazon.com/gp/product/B000U88T28/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B000U88T28"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B000U88T28&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>Sneak and spy RPGs are a lot of fun, especially when you&rsquo;re first getting
started. This one has very nice graphics, and a pretty reasonable set of
controls and mechanics. Like many games that are also console games, there
aren&rsquo;t as many controls and options as a big MMO, and that is good for this
kind of game. Crouch, sneak, aim, shoot, stab, etc.</p>

<p>I did really enjoy this game but there is one thing that really annoyed me and
made me stop playing. Ubisoft totally screwed the pooch on making the wireless
Xbox controller for PC work. And since they don&rsquo;t give you any configuration
options for the controller buttons, you pretty much have to use the keyboard.
(Apparently it works fine with a wired controller but that isn&rsquo;t what I have.
Also there are some hacks here and there and I almost got one to work, but then
I gave up and started playing something else.)</p>

<p>A good game. Good playstyle, an interesting story line, and it is always fun to
grab a bad guy from outside a window and toss him down four floors to the
street below.</p>

<h3 id="deus-ex-human-revolution---pc">Deus Ex: Human Revolution - PC</h3>

<p><strong>Metacritic Score: 90</strong></p>

<p><a href="http://www.amazon.com/gp/product/B005HRZ29K/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B005HRZ29K"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B005HRZ29K&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I am still playing this one on-and-off. Unlike Splinter Cell, this one works
perfectly with my Xbox wireless controller. It has the stealthy aspects of
Splinter Cell. You also can just run and gun if you want to play that way, but
it is not nearly as much fun.</p>

<p>There is an achievement called Ghost that was fun to get. Basically you have to
get through an entire level without getting seen. And given that you do have to
take some people out in almost every level, that can be fun to pull off.</p>

<p>This one has some interesting sci-fi aspects and a recurring mini-game that
they call &ldquo;hacking&rdquo;, but that is basically dependent on whether you spent the
right talent points and get lucky with the <a href="http://en.wikipedia.org/wiki/Random_number_generator">RNG</a>.</p>

<p>A good game, probably will give it some more time.</p>

<h3 id="rage---pc">Rage - PC</h3>

<p><strong>Metacritic Score: 79</strong></p>

<p><a href="http://www.amazon.com/gp/product/B003ICGL7I/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B003ICGL7I"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B003ICGL7I&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I grabbed this one in another Steam weekend sale and it was a lot of fun for a
while. It is not-quite a rail shooter, but also not at all an open environment
game. Graphically it is clearly an Id game, as it just has that &ldquo;Doom feel&rdquo; so 
many of their titles have.</p>

<p>What really makes this game interesting is the constant transition between
vehicle fighting and dungeon-like instances. Any time you get a mission, you
have to drive there. Along the way you will do combat with bad guys also in
vehicles. This is fun.</p>

<p>There are also races you can do to get car upgrades, so if you want to just do
that, you can play this as if it were a car game. An in-town &ldquo;mission board&rdquo;
provides odd-jobs you can do, and some of those are fun. (I really liked being
the sniper defender from 300 yards away while the work crew repaired
something.)</p>

<p>As I mentioned before the dungeon instances are pretty much rail shooters (no
real path choices, just keep going forward blasting everying you see) but there
are a few puzzles to solve here and there.</p>

<h3 id="call-of-duty-modern-warfare-3---pc">Call of Duty: Modern Warfare 3 - PC</h3>

<p><strong>Metacritic Score: 78</strong></p>

<p><a href="http://www.amazon.com/gp/product/B00503EAG2/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B00503EAG2"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B00503EAG2&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I really liked multi-player CoD:MW2 until the aimbotting got so bad I had to
get one myself just to compete. And then it was boring.  CoD: Black Ops was
also pretty good, and while some people didn&rsquo;t like the way the servers worked
(publicly controlled with various rules and configs), I actually liked it. I
especially liked the so-called crouch servers where you had to always stay
crouched or you&rsquo;d get kicked. It slowed the game down and make it more
strategic as opposed to just full-speed run and gun.</p>

<p>I was optimistic when Modern Warfare 3 was announced, but unfortunately I think
I played it for less than a month. Like all of the CoD games, I really enjoyed
the single player campaign and the small-scale coop missions, but the MP was
all crazy run-and-gun. A few of the new battleground styles were fun, but I
just got bored.</p>

<p>Honestly, I&rsquo;m not sure I&rsquo;ll do another big only FPS for a while after this one.
I know the new CoD was just announced, but unless I can get it for cheap, I
will probably pass.</p>

<h3 id="old-school-racer---windows-phone">Old School Racer - Windows Phone</h3>

<p><strong>Metacritic Score: not available</strong></p>

<p><a href="http://www.windowsphone.com/en-US/apps/c6d95358-0548-4ca6-8470-1411783853eb"><img class="left" src="http://catalog.zune.net/v3.2/en-US/apps/c6d95358-0548-4ca6-8470-1411783853eb/primaryImage?width=240&amp;height=240&amp;resize=true" width="120" height="120" /></a></p>

<p>I saw my son playing some side scrolling motorcycle stunt game on his iPod
Touch, so decided to look around for something similar for my phone. There are
actually two versions of this game &ldquo;Old School Racer&rdquo; and &ldquo;Old School Racer
Classic&rdquo;. One is a typical outside motorcycle trials game and the other has all
kinds of weird environments.</p>

<p>I played both games all the way through. I found the tilt control to be passable
, it was easier to play with the left thumb lean bar.</p>

<p>A good game for a small screen, but it didn&rsquo;t feel as natural as I&rsquo;d like.
Still a pretty fun phone game for a buck.  (BTW, this game is apparently known
as OSR Unhinged on Xbox.)</p>

<h3 id="istunt-2---windows-phone--ios">iStunt 2 - Windows Phone &amp; iOS</h3>

<p><strong>Metacritic Score: 82</strong></p>

<p><a href="http://www.windowsphone.com/en-US/apps/843d756e-8e76-4cf7-a0e2-a65db913fa2b"><img class="left" src="http://catalog.zune.net/v3.2/en-US/apps/843d756e-8e76-4cf7-a0e2-a65db913fa2b/primaryImage?width=240&amp;height=240&amp;resize=true" width="120" height="120" /></a></p>

<p>I found this one purely by accident, and it is a blast. The same idea as other
stunt side scrollers like Old School Racer, but this time you&rsquo;re snowboarding.
It has grabs, flips, rail slides, gravity flips and zero-G zones.</p>

<p>This one I played through in both Stunt and Time Trial mode, of course getting
all stars on all levels. This is a bit of an obsession for me with games that
keep score this way, and that includes things like Angry Birds and Cut the
Rope.  I simply can&rsquo;t stop until I get all the points. I will do a level for as
long as it takes to get that last star, and I didn&rsquo;t get bored of this one too
quickly to achieve that.</p>

<p>For $3 it is a bit more money than some other phone games, but it is worth it.
The tilt controls are natural and work well. The stunts are fun, and often
required to get that last star. My son has it on his iPod and it was fun for us
to be sitting side-by-side working on levels. Recommended.</p>

<p>(You can play <a href="http://www.miniclip.com/games/istunt-2/en/">the Flash version of this game</a> on the Miniclip site but without the
tilt and shake of the phone, it feels totally different.)</p>

<h3 id="trials-hd-and-trials-evolution">Trials HD and Trials Evolution</h3>

<p><strong>Metacritic Score: 86-91</strong></p>

<p><a href="http://www.amazon.com/gp/product/B002L3RUKC/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B002L3RUKC"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=B002L3RUKC&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I knew there had to be a good stunt trials kind of game on the Xbox and these
two are it. It has good graphics and physics, easy levels and ridiculously hard
levels. It has rewards for achievements, even some that cross games.  (I really
wanted the Micro Donkey 60cc mini bike in Evolution so had to go back to trials
HD to get the Unyielding achievement. That one was hard!)</p>

<p>If your Xbox Live friends also have the game, you can see an in-game marker
indicating their best time &amp; position on that track. It can be a bit
distracting though as you try to make sure you don&rsquo;t get &ldquo;beat&rdquo; by the your
friends.</p>

<p>Evolution added a bunch of new things to the classic HD motorcycle game. There
are skiing courses, a very cool map editor (you can share maps), and even
mutliplayer races (my son always wins).</p>

<p>I&rsquo;m still playing through these two games because, as you may have guessed, I
still don&rsquo;t have gold medals on all the levels. Oh, and the hard+ levels are
frigging HARD!</p>

<h2 id="other-games-of-interest">Other Games of Interest</h2>

<h3 id="star-wars-the-old-republic">Star Wars: The Old Republic</h3>

<p><strong>Metacritic Score: 85</strong></p>

<p><a href="http://www.amazon.com/gp/product/B001CWXAP2/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B001CWXAP2"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B001CWXAP2&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>The newest Wow-killer certainly took a dent out of it. A lot of my friends left
Wow to play SW:ToR, but many of them also came back after the leveling part was
over. From what people are telling me, the story line and leveling are second
to none for an MMO, but the end game raiding experience is way behind.</p>

<p>I have an account, but still haven&rsquo;t activated it. Let&rsquo;s see if it lasts longer
than the other so-called Wow killers from the past two years.</p>

<h3 id="fallout-new-vegas">Fallout: New Vegas</h3>

<p><strong>Metacritic Score: 84</strong></p>

<p><a href="http://www.amazon.com/gp/product/B002SU4QG4/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B002SU4QG4"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B002SU4QG4&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I bought this one a long time ago when I was playing Fallout 3, but by the time
I was done with that, I just couldn&rsquo;t bring myself to keep going. It is still
sitting there in my Steam library waiting for me and maybe I&rsquo;ll go back and
give it a try some day.</p>

<h3 id="just-cause-2">Just Cause 2</h3>

<p><strong>Metacritic Score: 84</strong></p>

<p><a href="http://www.amazon.com/gp/product/B004UHSGUA/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B004UHSGUA"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B004UHSGUA&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I actually played this one three or four times and it was interesting. I like
the grapple thing, but as I was playing Skyrim as the time I gave up pretty
early. It has a bunch of other interesting gameplay mechanics too, so if you
can get it cheap, it might be worth a try.</p>

<p>We&rsquo;ll see if it draws me back.</p>

<h3 id="half-life-2-episode-3">Half-Life 2: Episode 3</h3>

<p><strong>Metacritic Score: not yet released</strong></p>

<p><img class="left" src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Half-Life_2_Logo.svg/120px-Half-Life_2_Logo.svg.png" /></p>

<p>I absolutely <strong>loved</strong> the entire Half-Life series and played them all through
to the end, even the Lost Coast mini expansion. Occasionally scary, but always
fun, I&rsquo;ve been eagerly waiting for any new at all from Valve about the
continuation of this franchise. These are probably the only first person RPGs
I&rsquo;ve actually stuck with end-to-end and come back for more. </p>

<p>Hopefully this isn&rsquo;t a dead series, because I think there is more good stuff
possible here. Valve has made <a href="http://www.escapistmagazine.com/news/view/108821-Valve-Says-Hang-In-There-For-Half-Life-2-Episode-3">occasional statements about it</a>, but still
nothing concrete. And given how well Portal 2 did last year, I wonder if 
they will just go back to that pool to drink. (Of course, Portal 2 was a
great game, and the coop was super-fun, but I want some HL2:E3!!)</p>

<h3 id="battlefield-3">Battlefield 3</h3>

<p><strong>Metacritic Score: 89</strong></p>

<p><a href="http://www.amazon.com/gp/product/B003O6G5TW/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B003O6G5TW"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B003O6G5TW&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>I sorta wish I&rsquo;d bought this instead of CoD:MW3, but&hellip; well&hellip; I didn&rsquo;t. And
by the time I gave up MW3, I wasn&rsquo;t interested in another combat &amp; warfare FPS.</p>

<p>But friends have told me that the vehicle combat is great, so I may go back and
get this if I can grab it for a steal.</p>

<h3 id="call-of-duty-black-ops-ii">Call of Duty: Black Ops II</h3>

<p><strong>Metacritic Score: not yet released</strong></p>

<p><a href="http://www.amazon.com/gp/product/B007XVTR12/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B007XVTR12"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B007XVTR12&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>This was just announced and will ship before the holiday season this year.
Apparently set in a much more futuristic setting than the classic CoD games,
this one could be very interesting. Since it is a Treyarch game like he last
Black Ops, I&rsquo;m hoping it has the same kind of public multiplayer server
community, so it doesn&rsquo;t become just another speed run-and-gun game. We&rsquo;ll see.</p>

<p>It is available for pre-order pretty much everywhere, but given that the release
date isn&rsquo;t until November, I&rsquo;m in no rush to hand over my money.</p>

<h3 id="halo-reach">Halo: Reach</h3>

<p><strong>Metacritic Score: 91</strong></p>

<p><a href="http://www.amazon.com/gp/product/B002BSA20M/ref=as_li_ss_il?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B002BSA20M"><img class="left" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B002BSA20M&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=peterprovosto-20&amp;ServiceVersion=20070822" /></a></p>

<p>Like some of the others listed here at the end, I own this one but haven&rsquo;t
really sat down to play it yet. I did find aiming to be a bit hard with the
controller but that was probably just lack of practice, since I don&rsquo;t generally
play FPS games on the Xbox.</p>

<h2 id="wrap-up">Wrap up</h2>

<p>I&rsquo;m still looking for a good MMO to take the place Wow has had for me for the
last few years, but there are certainly a lot of good games out there to play
and I&rsquo;m having fun trying them all out. I love that so many of the games now
have trials you can play before committing the real money. The number of games
I&rsquo;ve tried but not purchased on the phone and Xbox is crazy.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Kata - The only way to learn TDD]]></title>
    <link href="http://www.peterprovost.org//blog/2012/05/02/kata-the-only-way-to-learn-tdd/"/>
    <updated>2012-05-02T23:34:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/05/02/kata-the-only-way-to-learn-tdd</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-02-the-only-way-to-learn-tdd-kata/Emmanuelle-Fumonde-Demonstration.jpg" title="'Source: &#104;&#116;&#116;&#112;&#58;&#47;&#47;&#101;&#110;&#46;&#119;&#105;&#107;&#105;&#112;&#101;&#100;&#105;&#97;&#46;&#111;&#114;&#103;&#47;&#119;&#105;&#107;&#105;&#47;&#70;&#105;&#108;&#101;&#58;&#69;&#109;&#109;&#97;&#110;&#117;&#101;&#108;&#108;&#101;&#45;&#70;&#117;&#109;&#111;&#110;&#100;&#101;&#45;&#101;&#110;&#45;&#100;&#37;&#67;&#51;&#37;&#65;&#57;&#109;&#111;&#110;&#115;&#116;&#114;&#97;&#116;&#105;&#111;&#110;&#46;&#106;&#112;&#103;'" /></p>

<p>Lately I&rsquo;ve been asked by more and more people inside Microsoft to help them
really learn to do TDD. Sure, they&rsquo;ve read the books, and probably some blog
posts and articles, but they are struggling to figure it out.</p>

<p>But with so many people talking about how good it is they are frustrated that
when they try to apply it at work, in their day job, it just doesn&rsquo;t seem to
work out.</p>

<p>My guidance to them is simple. Do a TDD kata every morning for two weeks. Limit
yourself to 30 minutes each morning.  Then pick another kata and do it again.</p>

<!-- More -->

<h2 id="what-is-a-tdd-kata">What is a TDD Kata?</h2>

<p><img class="left" src="http://www.peterprovost.org//images/blog/2012-05-02-the-only-way-to-learn-tdd-kata/Red-Green-Refactor-Bunny.png" /></p>

<p>Kata is a Japanese word meaning &ldquo;form&rdquo;, and in the martial arts it describes a
choreographed pattern of movements used to train yourself to the level of
muscle memory. I study Kenpo myself and have a number of kata that I practice
regularly both in training and at home.</p>

<p>The idea of kata for software development was originally coined by Dave Thomas,
co-author of one of my favorite developer handbooks 
<a href="http://www.amazon.com/gp/product/020161622X/ref=as_li_ss_tl?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=020161622X">The Pragmatic Programmer: From Journeyman to Master</a>.
The idea is the same as in martial arts, practice and repetition to hone
the skills and lock in the patterns.</p>

<p>One of the reasons TDD is hard for newcomers is that it is very explicit about
the baby-step nature of the changes you make to the code. You simply are not
allowed to add anything more than the current tests require. And for
experienced developers, this is very hard to do, especially when working on
<strong><em>real code</em></strong>.</p>

<p>By using the small but precise nature of the kata to practice these skills, you
can get over this roadblock in a safe way where you understand that the purpose
for the baby steps is to learn the movements.</p>

<h2 id="every-day-for-two-weeks-really">Every day for two weeks? Really?</h2>

<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-02-the-only-way-to-learn-tdd-kata/Wristwatch.jpg" title="30 minutes a day" alt="Source: &#104;&#116;&#116;&#112;&#58;&#47;&#47;&#101;&#110;&#46;&#119;&#105;&#107;&#105;&#112;&#101;&#100;&#105;&#97;&#46;&#111;&#114;&#103;&#47;&#119;&#105;&#107;&#105;&#47;&#70;&#105;&#108;&#101;&#58;&#84;&#105;&#109;&#101;&#120;&#95;&#84;&#53;&#69;&#57;&#48;&#49;&#95;&#73;&#114;&#111;&#110;&#109;&#97;&#110;&#95;&#84;&#114;&#105;&#97;&#116;&#104;&#108;&#111;&#110;&#95;&#51;&#48;&#95;&#76;&#97;&#112;&#95;&#70;&#76;&#73;&#88;&#46;&#106;&#112;&#103;" /></p>

<p>Actually, I would encourage you to do one every day for the rest of your
programming career, but that might be a bit extreme for some people. In my
martial arts training, we warm up with forms and kata, so why not have a nice
regular warm-up exercise to do while your morning coffee takes hold?</p>

<p>My recommendation to people is to do a 30-minute kata every morning for two
weeks. Then pick another one and do it every day for two weeks. Continue on.</p>

<p>I don&rsquo;t recommend people start using at work, on their actual work projects
until they feel like they are ready. I can&rsquo;t tell you when that will be, but
you will know. It may be after one week or six, but until you feel like you are
very comfortable with the rhythm of the TDD cycle, you should wait. To do
otherwise is like entering into a martial arts tournament with no skills.</p>

<p>Oh and did I mention that kata are actually fun?</p>

<h2 id="using-a-kata-to-learn">Using a kata to learn</h2>

<p>I still do kata all the time, although I don&rsquo;t do them every day.  Sometimes I
do one that I have already memorized and sometimes I go hunt down a new one.
Sometime I make one up and do that over and over for a few days.</p>

<p>Most commonly, when I do a kata these days, it is to learn a new technology. As
an example, a while back I wanted to learn more about <a href="http://subspec.codeplex.com/">SubSpec</a> and <a href="http://shouldly.github.com/">Shouldly</a>,
which are a nice way to do BDD style development on top of xUnit.net. I could
have just played with it for five minutes, but instead I did the string
calculator kata every day for a week.</p>

<p>By doing that I actually learned a lot more about SubSpec than I would have
learned otherwise. It really helped me understand the different between their
Assert and Observe methods, for example.</p>

<p>I&rsquo;ve also used Kata to learn new underlying technologies. When WPF and
Silverlight were first getting attention, and the Model-View-ViewModel (MVVM)
approach appeared, I developed a quick kata where I create a ViewModel from
scratch for a simple but imaginary system. I did it the same way every day for
a week and MVVM became very internalized for me.</p>

<p>Then when XAML-based Metro Style app development appeared for Windows 8, I did
them again, but this time entirely within the constraints of Windows 8
development. I reused my existing practice form to learn a new way of
developing apps. And it worked great.</p>

<h2 id="what-katas-are-there-which-should-i-do-first">What katas are there? Which should I do first?</h2>

<p>I generally recommend one of two different kata for getting started: The
Bowling Game or the String Calculator. Most recently, I&rsquo;ve been demonstrating
the String Calculator over the Bowling Game because for people who didn&rsquo;t grow
up in the US, 10-pin bowling can be a bit weird to describe.</p>

<h3 id="the-bowling-game-kata">The Bowling Game Kata</h3>

<p>As I said, if you live in the US or Canada, this one is easy to understand. If 
not, you might want to look at the next one.</p>

<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-02-the-only-way-to-learn-tdd-kata/bowling.jpg" title="10-pin Bowling" alt="Source: &#104;&#116;&#116;&#112;&#58;&#47;&#47;&#101;&#110;&#46;&#119;&#105;&#107;&#105;&#112;&#101;&#100;&#105;&#97;&#46;&#111;&#114;&#103;&#47;&#119;&#105;&#107;&#105;&#47;&#70;&#105;&#108;&#101;&#58;&#66;&#111;&#119;&#108;&#105;&#110;&#103;&#95;&#45;&#95;&#97;&#108;&#98;&#117;&#114;&#121;&#46;&#106;&#112;&#103;" /></p>

<p>The essence of this kata, popularized by Uncle Bob Martin, is to create a
scoring engine for 10-pin Bowling. In 10-pin bowling you have ten frames where
you can roll one, two (or three) balls and score the pins that you knock down.
Sounds simple right?</p>

<p>Except the scoring is weird. You&rsquo;d think 10 pins and ten frames would yield a
highest possible score of 100, but you&rsquo;d be wrong.  The lowest possible score
(all misses) is zero, but the highest possible score is 300. For more
information about 10-pin bowling scoring I suggest reading the 
<a href="http://en.wikipedia.org/wiki/10-Pin_Bowling#Scoring">Wikipedia page</a>. But this is exactly why it is a fun system to model for our kata.</p>

<p>Uncle Bob breaks this kata down into the following five tests:</p>

<ol>
  <li>Gutter game scores zero - When you roll all misses, you get a total
score of zero.</li>
  <li>All ones scores 20 - When you knock down one pin with each ball, your
total score is 20.</li>
  <li>A spare in the first frame, followed by three pins, followed by
all misses scores 16.</li>
  <li>A strike in the first frame, followed by three and then four pins,
followed by all misses, scores 24.</li>
  <li>A perfect game (12 strikes) scores 300.</li>
</ol>

<p>He has some very explicit refactorings that you are to perform while
implementing the third and fourth tests, and doing these is important.
I&rsquo;ve always found it weird that the final test just passes when you do
the first four right (this smells a bit of over-implementing test four)
but I&rsquo;ve never really found a way to do #3 and then #4 that didn&rsquo;t result
in #5 just passing. Oh well.</p>

<p>You can read <a href="http://www.butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata">Uncle Bob&rsquo;s wiki page</a> on the topic for the canonical definition.</p>

<h3 id="the-string-calculator-kata">The String Calculator Kata</h3>

<p>Like the Bowling Kata, this kata, made popular by Roy Osherove, comes with a
precise set of steps to follow. The essence is a method that given a delimited
string, returns the sum of the values. I&rsquo;ve always preferred my kata to define
the tests I will follow every time through the exercise, so here are the tests
I use for this one:</p>

<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-05-02-the-only-way-to-learn-tdd-kata/HP_65.jpg" title="HP 65 Calculator" alt="Source: &#104;&#116;&#116;&#112;&#58;&#47;&#47;&#101;&#110;&#46;&#119;&#105;&#107;&#105;&#112;&#101;&#100;&#105;&#97;&#46;&#111;&#114;&#103;&#47;&#119;&#105;&#107;&#105;&#47;&#70;&#105;&#108;&#101;&#58;&#84;&#105;&#109;&#101;&#120;&#95;&#84;&#53;&#69;&#57;&#48;&#49;&#95;&#73;&#114;&#111;&#110;&#109;&#97;&#110;&#95;&#84;&#114;&#105;&#97;&#116;&#104;&#108;&#111;&#110;&#95;&#51;&#48;&#95;&#76;&#97;&#112;&#95;&#70;&#76;&#73;&#88;&#46;&#106;&#112;&#103;" /></p>

<ol>
  <li>An empty string returns zero</li>
  <li>A single number returns the value</li>
  <li>Two numbers, comma delimited, returns the sum</li>
  <li>Two numbers, newline delimited, returns the sum</li>
  <li>Three numbers, delimited either way, returns the sum</li>
  <li>Negative numbers throw an exception</li>
  <li>Numbers greater than 1000 are ignored</li>
  <li>A single char delimiter can be defined on the first line (e.g. <code>//#</code> for a
&lsquo;#&rsquo; as the delimiter)</li>
  <li>A multi char delimiter can be defined on the first line (e.g. <code>//[###]</code> for
&lsquo;###&rsquo; as the delimiter)</li>
  <li>Many single or multi-char delimiters can be defined (each wrapped in square
brackets)</li>
</ol>

<p>I rarely bother with Test #10 when I do it, because it feels like a big step
to take all at once, but Roy does include it in his definition, and I have 
it in my kata notebook.</p>

<p>To read Roy&rsquo;s original post describing the kata, or to see a bunch 
of video demonstrations (in just as many languages) visit <a href="http://osherove.com/tdd-kata-1/">his page
on the topic</a>.</p>

<h2 id="more-katas-to-choose-from">More katas to choose from</h2>

<p>A couple of others that I&rsquo;ve used when training or for my own fun and
benefit are:</p>

<ul>
  <li><a href="http://www.butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata">The Prime Factors Kata</a> - another by Bob Martin</li>
  <li><a href="http://codingkata.net/Katas/Beginner/FizzBuzz">Fizz Buzz Kata</a> - the crazy (drinking?) game where you replace certain numbers
 or multiples of numbers with nonsense words</li>
</ul>

<p>Most kata share a few key attributes. First they are simple to describe (well
maybe not 10-pin bowling). The problem is intentionally simplified to one
thing. Secondly, they should come with the set of tests you are to use to drive
the design. Sometimes they will also come with specific or recommended
refactorings to perform at certain steps.</p>

<p>There are many sites out there that have catalogs of kata to dig through, but
I really do recommend doing them one at a time for at least a week before
picking up another one. Here are a few of those lists, and I&rsquo;m sure there are
many more:</p>

<ul>
  <li><a href="http://codekata.pragprog.com/codekata/">Dave Thomas&rsquo; list of katas</a></li>
  <li><a href="http://codingdojo.org/cgi-bin/wiki.pl?KataCatalogue">CodingDojo.org Kata Index</a></li>
  <li><a href="http://tddkatas.codeplex.com/">TDDKatas @ CodePlex.com</a></li>
  <li><a href="http://codingkata.net/">CodingKata.net</a> - all C#, in browser, no IDE required</li>
</ul>

<h2 id="what-are-you-waiting-for">What are you waiting for?</h2>

<p>I&rsquo;ve been doing kata for a very long time and I really do recommend them
as a tool for learning what TDD really feels like. Give it a try, and use
my guidelines: every morning, time-boxed to 30 mins, the same kata for one
or two weeks, then pick a new one and repeat.</p>

<p>Hopefully you will get as much value from them as I do.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Visual Studio Fakes Part 2 - Shims]]></title>
    <link href="http://www.peterprovost.org//blog/2012/04/25/visual-studio-11-fakes-part-2/"/>
    <updated>2012-04-25T09:00:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/04/25/visual-studio-11-fakes-part-2</id>
    <content type="html"><![CDATA[<p>Let me start by saying <strong>Shims are evil</strong>. But they are evil by design.  They
let you do things you otherwise couldn&rsquo;t do, which is very powerful.  They let
you do things you might not want to do, and might know you shouldn&rsquo;t do, but
because of the real world of software, you have to do.</p>

<h2 id="the-catch-22-of-refactoring-to-enable-unit-testing">The Catch-22 of Refactoring to Enable Unit Testing</h2>

<p>In <a href="http://www.peterprovost.org//blog/2012/04/15/visual-studio-11-fakes-part-1/">the first part</a> of my series on VS11 Fakes, I reviewed Stubs, which are
a simple way of creating concrete implementations of interfaces and abstract
classes for use in unit tests.</p>

<p>But sometimes it happens that you have to test a method where the dependencies
can&rsquo;t be simply injected via an interface. It might be that your code depends
on an external system like SharePoint, or simply that the code news up or uses
a concrete object inside the method, where you can&rsquo;t easily replace it.</p>

<p>The unit testing agilista have always said, &ldquo;Refactor your code to make it more
testable,&rdquo; but therein lies the rub. I will again refer to the esteemed Martin
Fowler for a quote:</p>

<!-- more -->

<blockquote><p>Refactoring is a disciplined technique for restructuring an existing body of<br />code, altering its internal structure without changing its external behavior.</p><footer><strong>Martin Fowler</strong> <cite><a href="http://martinfowler.com/refactoring/">martinfowler.com/refactoring/&hellip;</a></cite></footer></blockquote>

<p>How do you know if you are changing its external behavior? &ldquo;Simple!&rdquo; says the
Agilista, &ldquo;You know you didn&rsquo;t change it as long as your unit tests still
pass.&rdquo; But wait&hellip; we don&rsquo;t have unit tests yet for this code (that&rsquo;s what
we&rsquo;re trying to fix), so I&rsquo;m stuck&hellip; <a href="http://en.wikipedia.org/wiki/Catch-22_(logic)">Catch-22</a>.</p>

<p>I have to quote <a href="http://www.amazon.com/gp/product/1451626657/ref=as_li_ss_tl?ie=UTF8&amp;tag=peterprovosto-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1451626657">Joseph Heller&rsquo;s masterwork</a> just for fun:</p>

<blockquote><p>There was only one catch and that was Catch-22, which specified that a concern<br />for one's safety in the face of dangers that were real and immediate was the<br />process of a rational mind. Orr was crazy and could be grounded. All he had to<br />do was ask; and as soon as he did, he would no longer be crazy and would have<br />to fly more missions. Orr would be crazy to fly more missions and sane if he<br />didn't, but if he were sane he had to fly them. If he flew them he was crazy<br />and didn't have to; but if he didn't want to he was sane and had to. Yossarian<br />was moved very deeply by the absolute simplicity of this clause of Catch-22 and<br />let out a respectful whistle.</p><footer><strong>Joseph Heller</strong> <cite>Catch-22 Chapter 5</cite></footer></blockquote>

<p>He&rsquo;s crazy if he wants to go fight, but if he says he really didn&rsquo;t want to
fight then he isn&rsquo;t crazy and so needs to go fight. We have the same problem.
We can&rsquo;t test because it needs refactoring, but we can&rsquo;t refactor because we
don&rsquo;t have tests.</p>

<h2 id="shim-your-way-out-of-the-paradox">Shim Your Way Out of the Paradox</h2>

<h3 id="example-1--">Example 1 -</h3>

<p>To see what this really looks like, let&rsquo;s look at some code (continued from the
example in Part 1).</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>"Untestable" System Under Test Code</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">namespace</span> <span class="nn">ShimsDemo.SystemUnderTest</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">public</span> <span class="k">class</span> <span class="nc">CustomerViewModel</span> <span class="p">:</span> <span class="n">ViewModelBase</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="k">private</span> <span class="n">Customer</span> <span class="n">customer</span><span class="p">;</span>
</span><span class="line">      <span class="k">private</span> <span class="k">readonly</span> <span class="n">ICustomerRepository</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="nf">CustomerViewModel</span><span class="p">(</span><span class="n">Customer</span> <span class="n">customer</span><span class="p">,</span> <span class="n">ICustomerRepository</span> <span class="n">repository</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">customer</span> <span class="p">=</span> <span class="n">customer</span><span class="p">;</span>
</span><span class="line">         <span class="k">this</span><span class="p">.</span><span class="n">repository</span> <span class="p">=</span> <span class="n">repository</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="k">get</span> <span class="p">{</span> <span class="k">return</span> <span class="n">customer</span><span class="p">.</span><span class="n">Name</span><span class="p">;</span> <span class="p">}</span>
</span><span class="line">         <span class="k">set</span>
</span><span class="line">         <span class="p">{</span>
</span><span class="line">            <span class="n">customer</span><span class="p">.</span><span class="n">Name</span> <span class="p">=</span> <span class="k">value</span><span class="p">;</span>
</span><span class="line">            <span class="n">RaisePropertyChanged</span><span class="p">(</span><span class="s">&quot;Name&quot;</span><span class="p">);</span>
</span><span class="line">         <span class="p">}</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">
</span><span class="line">      <span class="k">public</span> <span class="k">void</span> <span class="nf">Save</span><span class="p">()</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="n">customer</span><span class="p">.</span><span class="n">LastUpdated</span> <span class="p">=</span> <span class="n">DateTime</span><span class="p">.</span><span class="n">Now</span><span class="p">;</span>  <span class="c1">// HOW DO WE TEST THIS?</span>
</span><span class="line">         <span class="n">customer</span> <span class="p">=</span> <span class="n">repository</span><span class="p">.</span><span class="n">SaveOrUpdate</span><span class="p">(</span><span class="n">customer</span><span class="p">);</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>There is one minor change from our previous example. We are now setting the
<code>LastUpdated</code> property on the <code>Customer</code> object before passing it to the
repository. (I know this might not be the best way to do this, but go with
me&hellip;)</p>

<p><strong><em>How do we test that Save sets the correct value to LastUpdated?</em></strong></p>

<p>You might start by writing a test like this. This uses the same Stubs techniques
I showed in the last article and tries hard to deal with the variable nature of
the <code>LastUpdated</code> property.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>A Not-so-good Test for LastUpdated</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">class</span> <span class="nc">CustomerViewModelTests</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"><span class="na">   [Fact]</span>
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="nf">SaveShouldSetTheCorrectLastUpdatedDate_WithoutShim</span><span class="p">()</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="c1">// Arrange</span>
</span><span class="line">      <span class="n">var</span> <span class="n">savedCustomer</span> <span class="p">=</span> <span class="k">default</span><span class="p">(</span><span class="n">Customer</span><span class="p">);</span> <span class="c1">// null</span>
</span><span class="line">      <span class="n">var</span> <span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubICustomerRepository</span>
</span><span class="line">            <span class="p">{</span>
</span><span class="line">               <span class="n">SaveOrUpdateCustomer</span> <span class="p">=</span> <span class="n">customer</span> <span class="p">=&gt;</span> <span class="n">savedCustomer</span> <span class="p">=</span> <span class="n">customer</span>
</span><span class="line">            <span class="p">};</span>
</span><span class="line">      <span class="n">var</span> <span class="n">actualCustomer</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Customer</span>
</span><span class="line">            <span class="p">{</span>
</span><span class="line">               <span class="n">Id</span> <span class="p">=</span> <span class="m">1</span><span class="p">,</span>
</span><span class="line">               <span class="n">Name</span> <span class="p">=</span> <span class="s">&quot;Sample Customer&quot;</span><span class="p">,</span>
</span><span class="line">               <span class="n">LastUpdated</span><span class="p">=</span><span class="n">DateTime</span><span class="p">.</span><span class="n">MinValue</span>
</span><span class="line">            <span class="p">};</span>
</span><span class="line">      <span class="n">var</span> <span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomerViewModel</span><span class="p">(</span><span class="n">actualCustomer</span><span class="p">,</span> <span class="n">repository</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Act</span>
</span><span class="line">      <span class="n">var</span> <span class="n">now</span> <span class="p">=</span> <span class="n">DateTime</span><span class="p">.</span><span class="n">Now</span><span class="p">;</span>
</span><span class="line">      <span class="n">viewModel</span><span class="p">.</span><span class="n">Save</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Assert</span>
</span><span class="line">      <span class="n">Assert</span><span class="p">.</span><span class="n">NotNull</span><span class="p">(</span><span class="n">savedCustomer</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// We will use a 10ms &quot;window&quot; to confirm that the date is &quot;close enough&quot;</span>
</span><span class="line">      <span class="c1">// to what we expect. Not ideal, but it should work... most of the time.</span>
</span><span class="line">      <span class="n">var</span> <span class="n">delta</span> <span class="p">=</span> <span class="n">Math</span><span class="p">.</span><span class="n">Abs</span><span class="p">((</span><span class="n">savedCustomer</span><span class="p">.</span><span class="n">LastUpdated</span> <span class="p">-</span> <span class="n">now</span><span class="p">).</span><span class="n">TotalMilliseconds</span><span class="p">);</span>
</span><span class="line">      <span class="k">const</span> <span class="kt">int</span> <span class="n">accuracy</span> <span class="p">=</span> <span class="m">10</span><span class="p">;</span> <span class="c1">// milliseconds</span>
</span><span class="line">      <span class="n">Assert</span><span class="p">.</span><span class="n">True</span><span class="p">(</span><span class="n">delta</span> <span class="p">&lt;=</span> <span class="n">accuracy</span><span class="p">,</span> <span class="s">&quot;LastUpdated was not appx equal to expected&quot;</span><span class="p">);</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>There are, of course, a few issues with this code, most notably the bit below
the comment explaining the 10ms window. How small can we make the <code>accuracy</code>
variable before the test starts to fail? How close is close enough?  What about
when the <code>viewModel.Save()</code> code does extra work eating up some of that time?</p>

<p>A better way to test this is to use a Shim to override <code>System.DateTime</code>, causing
it to always return a predictable value. That test would look something like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>A Better Test for LastUpdated</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">class</span> <span class="nc">CustomerViewModelTests</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"><span class="na">   [Fact]</span>
</span><span class="line">   <span class="k">public</span> <span class="k">void</span> <span class="nf">SaveShouldSetTheCorrectLastUpdatedDate_WithShim</span><span class="p">()</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="k">using</span> <span class="p">(</span><span class="n">ShimsContext</span><span class="p">.</span><span class="n">Create</span><span class="p">())</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="c1">// Arrange</span>
</span><span class="line">         <span class="n">var</span> <span class="n">savedCustomer</span> <span class="p">=</span> <span class="k">default</span><span class="p">(</span><span class="n">Customer</span><span class="p">);</span> <span class="c1">// null</span>
</span><span class="line">         <span class="n">var</span> <span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubICustomerRepository</span>
</span><span class="line">               <span class="p">{</span>
</span><span class="line">                  <span class="n">SaveOrUpdateCustomer</span> <span class="p">=</span> <span class="n">customer</span> <span class="p">=&gt;</span> <span class="n">savedCustomer</span> <span class="p">=</span> <span class="n">customer</span>
</span><span class="line">               <span class="p">};</span>
</span><span class="line">
</span><span class="line">         <span class="c1">// Make DateTime.Now always return midnight Jan 1, 2012</span>
</span><span class="line">         <span class="n">ShimDateTime</span><span class="p">.</span><span class="n">NowGet</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="k">new</span> <span class="n">DateTime</span><span class="p">(</span><span class="m">2012</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">1</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">         <span class="n">var</span> <span class="n">actualCustomer</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Customer</span>
</span><span class="line">               <span class="p">{</span>
</span><span class="line">                  <span class="n">Id</span> <span class="p">=</span> <span class="m">1</span><span class="p">,</span>
</span><span class="line">                  <span class="n">Name</span> <span class="p">=</span> <span class="s">&quot;Sample Customer&quot;</span><span class="p">,</span>
</span><span class="line">                  <span class="n">LastUpdated</span><span class="p">=</span><span class="n">DateTime</span><span class="p">.</span><span class="n">MinValue</span>
</span><span class="line">               <span class="p">};</span>
</span><span class="line">         <span class="n">var</span> <span class="n">viewModel</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomerViewModel</span><span class="p">(</span><span class="n">actualCustomer</span><span class="p">,</span> <span class="n">repository</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">         <span class="c1">// Act</span>
</span><span class="line">         <span class="n">viewModel</span><span class="p">.</span><span class="n">Save</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">         <span class="c1">// Assert</span>
</span><span class="line">         <span class="n">Assert</span><span class="p">.</span><span class="n">NotNull</span><span class="p">(</span><span class="n">savedCustomer</span><span class="p">);</span>
</span><span class="line">         <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span> <span class="k">new</span> <span class="n">DateTime</span><span class="p">(</span><span class="m">2012</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">1</span><span class="p">),</span> <span class="n">savedCustomer</span><span class="p">.</span><span class="n">LastUpdated</span> <span class="p">);</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>On line 21 we use Shims to override the <code>Now</code> static property getter on the
DateTime object. This replacement is for all calls to <code>DateTime.Now</code> in the
AppDomain, which is why we are required to provide a scoping region for our
replacements with the <code>ShimsContext.Create()</code> block.</p>

<p>But now this test is no longer &ldquo;flaky&rdquo;. It has no dependency on your system
clock, on the length of time that <code>Save()</code> or any other code takes to run. We
have controlled the environment and isolated this test from the external
dependency on <code>DateTime.Now</code>.</p>

<p>This example shows one issue that can make code difficult to test, but of
course there are more.</p>

<h3 id="example-2---the-hidden-object-instance">Example 2 - The hidden object instance</h3>

<p>Here&rsquo;s a different example showing another problem.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>How do I deal with that new statement?</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">StartTimer</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="n">var</span> <span class="n">timer</span> <span class="p">=</span> <span class="k">new</span> <span class="n">DispatcherTimer</span><span class="p">();</span>
</span><span class="line">   <span class="n">var</span> <span class="n">count</span> <span class="p">=</span> <span class="m">30</span><span class="p">;</span>
</span><span class="line">   <span class="n">timer</span><span class="p">.</span><span class="n">Tick</span> <span class="p">+=</span> <span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span> <span class="p">=&gt;</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="n">count</span> <span class="p">-=</span> <span class="m">1</span><span class="p">;</span>
</span><span class="line">         <span class="k">if</span> <span class="p">(</span><span class="n">count</span> <span class="p">==</span> <span class="m">0</span><span class="p">)</span>
</span><span class="line">         <span class="p">{</span>
</span><span class="line">            <span class="n">count</span> <span class="p">=</span> <span class="m">30</span><span class="p">;</span>
</span><span class="line">            <span class="n">RefreshData</span><span class="p">();</span>
</span><span class="line">         <span class="p">}</span>
</span><span class="line">      <span class="p">};</span>
</span><span class="line">   <span class="n">timer</span><span class="p">.</span><span class="n">Interval</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TimeSpan</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">);</span>
</span><span class="line">   <span class="n">timer</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>In this example, we have a method that is creating an instance of the
WPF <code>DispatchTimer</code> class. It uses the timer to track when 30 seconds
have elapsed and then it refreshes a property on the view model (presumably
this collection is bound in the XAML to a list on-screen) by calling 
<code>RefreshData()</code>.</p>

<p>How can we test this code? There are a few things we might like to test,
including:</p>

<ol>
  <li>Does it start the timer with a 1 second interval?</li>
  <li>Does it refresh the list only after 30 ticks have gone by?</li>
</ol>

<p>Let&rsquo;s look at how we might use Shims to create that first test.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Shimming out the DispatchTimer</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">TimerStartedWith30secInterval</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">using</span> <span class="p">(</span><span class="n">ShimsContext</span><span class="p">.</span><span class="n">Create</span><span class="p">())</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="c1">// Arrange</span>
</span><span class="line">      <span class="n">var</span> <span class="n">customer</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Customer</span><span class="p">();</span>
</span><span class="line">      <span class="n">var</span> <span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubICustomerRepository</span><span class="p">();</span>
</span><span class="line">      <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomerViewModel</span><span class="p">(</span><span class="n">customer</span><span class="p">,</span> <span class="n">repository</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">      <span class="n">var</span> <span class="n">span</span> <span class="p">=</span> <span class="k">default</span><span class="p">(</span><span class="n">TimeSpan</span><span class="p">);</span>
</span><span class="line">      <span class="n">var</span> <span class="n">startCalled</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">      <span class="n">ShimDispatcherTimer</span><span class="p">.</span><span class="n">AllInstances</span><span class="p">.</span><span class="n">IntervalSetTimeSpan</span> <span class="p">=</span> <span class="p">(</span><span class="n">@this</span><span class="p">,</span> <span class="n">ts</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">span</span> <span class="p">=</span> <span class="n">ts</span><span class="p">;</span>
</span><span class="line">      <span class="n">ShimDispatcherTimer</span><span class="p">.</span><span class="n">AllInstances</span><span class="p">.</span><span class="n">Start</span> <span class="p">=</span> <span class="p">(</span><span class="n">@this</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">startCalled</span> <span class="p">=</span> <span class="k">true</span><span class="p">;</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Act</span>
</span><span class="line">      <span class="n">sut</span><span class="p">.</span><span class="n">StartTimer</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Assert</span>
</span><span class="line">      <span class="n">Assert</span><span class="p">.</span><span class="n">Equal</span><span class="p">(</span><span class="n">TimeSpan</span><span class="p">.</span><span class="n">FromSeconds</span><span class="p">(</span><span class="m">1</span><span class="p">),</span> <span class="n">span</span><span class="p">);</span>
</span><span class="line">      <span class="n">Assert</span><span class="p">.</span><span class="n">True</span><span class="p">(</span><span class="n">startCalled</span><span class="p">);</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>In this example we use Shims ability to detour all future instances of an
object by using the special <code>AllInstances</code> property on the generated Shim
object. Since these methods are instance methods, our delegate has to include a
parameter for the &ldquo;this&rdquo; pointer. I like to use <code>@this</code> for that parameter to
remind me what it is, but I&rsquo;ve seen other people use <code>that</code> or <code>instance</code> if
you dislike the idea of using a C# keyword in your code.</p>

<p>We override the Interval property setter (which takes a timespan, hence the
name IntervalSetTimeSpan) and the Start method. By stashing away values into C#
closures, we can call <code>sut.StartTimer()</code> and then verify that what we expected
did in fact happen.</p>

<h3 id="example-3---using-shims-to-wrap-an-existing-instance">Example 3 - Using Shims to wrap an existing instance</h3>

<p>One more example will wrap up my introduction to Shims. </p>

<p>We will now take a look at how we might implement the second test in my
list above. We want to confirm that the <code>RefreshData</code> method is called
only after 30 ticks have elapsed.</p>

<p>Let&rsquo;s take a look at how we would test this using Shims. In this case we
will still need to control the <code>DispatchTimer</code>, but we will also need to 
override the implementation of <code>RefreshData()</code>, which is a method on the
class we&rsquo;re testing.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>Shimming a specific object instance</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[Fact]</span>
</span><span class="line"><span class="k">public</span> <span class="k">void</span> <span class="nf">RefreshTimerCallsRefreshAfter30Ticks</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="k">using</span> <span class="p">(</span><span class="n">ShimsContext</span><span class="p">.</span><span class="n">Create</span><span class="p">())</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="c1">// Arrange</span>
</span><span class="line">      <span class="n">var</span> <span class="n">customer</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Customer</span><span class="p">();</span>
</span><span class="line">      <span class="n">var</span> <span class="n">repository</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubICustomerRepository</span><span class="p">();</span>
</span><span class="line">      <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CustomerViewModel</span><span class="p">(</span><span class="n">customer</span><span class="p">,</span> <span class="n">repository</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">      <span class="n">var</span> <span class="n">refreshDataWasCalled</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span>
</span><span class="line">      <span class="k">new</span> <span class="nf">ShimCustomerViewModel</span><span class="p">(</span><span class="n">sut</span><span class="p">)</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="n">RefreshData</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="n">refreshDataWasCalled</span> <span class="p">=</span> <span class="k">true</span><span class="p">,</span>
</span><span class="line">      <span class="p">};</span>
</span><span class="line">
</span><span class="line">
</span><span class="line">		<span class="c1">// Setup our override of the Dispatch timer</span>
</span><span class="line">		<span class="c1">// We will store the tick handler so we can &quot;pump&quot; it</span>
</span><span class="line">		<span class="c1">// and noop override Interval and Start just to be safe</span>
</span><span class="line">		<span class="n">var</span> <span class="n">tick</span> <span class="p">=</span> <span class="k">default</span><span class="p">(</span><span class="n">EventHandler</span><span class="p">);</span>
</span><span class="line">		<span class="n">ShimDispatcherTimer</span><span class="p">.</span><span class="n">AllInstances</span><span class="p">.</span><span class="n">TickAddEventHandler</span> <span class="p">=</span> <span class="p">(</span><span class="n">@this</span><span class="p">,</span> <span class="n">h</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="n">tick</span> <span class="p">=</span> <span class="n">h</span><span class="p">;</span>
</span><span class="line">		<span class="n">ShimDispatcherTimer</span><span class="p">.</span><span class="n">AllInstances</span><span class="p">.</span><span class="n">IntervalSetTimeSpan</span> <span class="p">=</span> <span class="p">(</span><span class="n">@this</span><span class="p">,</span> <span class="n">ts</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="p">};</span>
</span><span class="line">		<span class="n">ShimDispatcherTimer</span><span class="p">.</span><span class="n">AllInstances</span><span class="p">.</span><span class="n">Start</span> <span class="p">=</span> <span class="p">(</span><span class="n">@this</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span> <span class="p">};</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Act</span>
</span><span class="line">      <span class="n">sut</span><span class="p">.</span><span class="n">StartTimer</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">      <span class="c1">// Assert</span>
</span><span class="line">      <span class="n">refreshDataWasCalled</span> <span class="p">=</span> <span class="k">false</span><span class="p">;</span> <span class="c1">// Clear out the any calls that happened before the tick()</span>
</span><span class="line">      <span class="k">for</span> <span class="p">(</span><span class="n">var</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="m">29</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span> <span class="n">tick</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="k">null</span><span class="p">);</span> <span class="c1">// Tick 29 times</span>
</span><span class="line">      <span class="n">Assert</span><span class="p">.</span><span class="n">False</span><span class="p">(</span><span class="n">refreshDataWasCalled</span><span class="p">);</span>
</span><span class="line">      <span class="n">tick</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="k">null</span><span class="p">);</span> <span class="c1">// Tick one more time</span>
</span><span class="line">      <span class="n">Assert</span><span class="p">.</span><span class="n">True</span><span class="p">(</span><span class="n">refreshDataWasCalled</span><span class="p">);</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>While nobody would call this a pretty test, it does what we asked it to do, and
has let us test one aspect of this method we otherwise wouldn&rsquo;t have been able
to test.</p>

<h2 id="resolving-the-catch-22">Resolving the Catch-22</h2>

<p>The Catch-22 that I mentioned at the start of this article can be restated like this:</p>

<ol>
  <li>You can&rsquo;t test the code because it has internal dependencies that make it untestable</li>
  <li>You need to refactor it to fix the dependency</li>
  <li>Since you don&rsquo;t have any tests for this code, you aren&rsquo;t safe to refactor</li>
  <li>See #1</li>
</ol>

<p>Hopefully you can see that Shims help you get out of this dilemma. With Shims we can
rework that list to something like this:</p>

<ol>
  <li>You can&rsquo;t test the code using traditional techniques because it has dependencies
that make it untestable.</li>
  <li>Before you can refactor it, you need to create a test that asserts its current
behavior.</li>
  <li>You can use Shims to create this characterization test, enabling you to refactor
safely.</li>
  <li>Once you have it under test, you can safely refactor and make the dependency
explicit and replaceable using traditional (e.g. interfaces and stubs) methods.</li>
  <li>The characterization test should continue to pass after performing the refactoring.</li>
  <li>Once the refactoring is complete, you can create new tests that do not use Shims
(but may use Stubs) to test the code.</li>
  <li>Then you can remove the Shims test.</li>
</ol>

<p>It is a few more steps, but it lets you have a clear, intentional way to refactor
code that otherwise would have required &ldquo;refactoring in the dark&rdquo;. The Catch-22 is
resolved and you can continue to improve the design and testability of your code.</p>

<p>I do recommend people go all the way with this approach and refactor the code
until the Shim-based test is no longer required. Every time you need to use
something like Shims to test something, it is telling you that you have a
design problem.  You probably have high-coupling and low cohesion in that
method.</p>

<p>Refactoring is the act of intentional design, and you should always take the
opportunity to make your design better. Shims can be used to get out of this
impasse, but if you don&rsquo;t think about the problem you will end up with a <a href="http://www.laputan.org/mud/">Big
Ball of Mud</a> for your design.</p>

<h2 id="some-final-words-about-shims">Some final words about Shims</h2>

<p>As the famous literary quote goes (Voltaire, Socrates and even Spider Man&rsquo;s Uncle Ben):</p>

<blockquote><p>With great power comes great responsibility.</p></blockquote>

<p>The examples I&rsquo;ve shown in this post are all examples of design flaws around
coupling and cohesion. Knowing that, we should always feel a bit &ldquo;dirty&rdquo; about
having to use Shims to test something. Every time we do it, we should remember
to go the extra mile afterwards and refactor it away if we can (my steps 4-7
above).</p>

<p>The Catch-22 of untestable code is real. Getting out of it is hard. Shims are
designed to help you get out of this trap. </p>

<p>Shims may be evil from a purist design and TDD sense, but in the real world we
are often faced with code we a) don&rsquo;t control or b) didn&rsquo;t write and which
doesn&rsquo;t have any tests. Use Shims to get out of that, but always continue on
and fix your design issues.</p>

<h2 id="conclusion">Conclusion</h2>

<p>Visual Studio 11 includes the new Fakes library for creating isolated unit
tests.  It includes two kinds of Fakes: </p>

<ul>
  <li><strong>Stubs</strong> for creating lightweight, fast running concrete classes for
interfaces and abstract classes your system uses. I reviewed Stubs in <a href="http://www.peterprovost.org//blog/2012/04/15/visual-studio-11-fakes-part-1/">my
last article</a>.</li>
  <li><strong>Shims</strong> for intercepting and detouring almost any .NET call. Shims are
particularly useful for removing internal dependencies of methods, and for
getting you out of the &ldquo;testability Catch-22&rdquo;.</li>
</ul>

<p>Hopefully you have found these two articles useful. For more information on Fakes
please take some time looking through the <a href="http://aka.ms/vs11-fakes">MSDN Documentation</a>.</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to avoid creating real tasks when unit testing async]]></title>
    <link href="http://www.peterprovost.org//blog/2012/04/23/how-to-avoid-creating-real-tasks-when-unit-testing-async/"/>
    <updated>2012-04-23T21:39:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/04/23/how-to-avoid-creating-real-tasks-when-unit-testing-async</id>
    <content type="html"><![CDATA[<p><strong><em>UPDATE: Stephen Toub pointed out that in .NET 4.5 you don&rsquo;t need my
<code>CreatePseudoTask()</code> helper method. See the bottom of this post for
more information.</em></strong></p>

<p>If you&rsquo;ve been coding in VS11 Beta with .NET 4.5 you may have started
experimenting with using async and await in your programs. You also probably
noticed a lot more of the APIs you consume are starting to expose asynchonous
methods using Task and Task&lt;T&gt;.</p>

<p>This technology let&rsquo;s use specify that operations are long running and should
be expected to not return quickly. You basically get to fire off async
processes without you having to manage the threads yourself.  Behind the
scenes, the necessary state machine code is created and, as they say, &ldquo;it just
works&rdquo;.</p>

<p><em>I would really recommend reading all the great posts by Stephen Toub and
others over on the <a href="http://blogs.msdn.com/b/pfxteam/">PFX Team blog</a>. And of course the MSDN Docs on 
the <a href="http://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx">Task Parallel Library</a> should be reviewed too.</em></p>

<p>But did you know that in VS11 Beta you can now create async unit tests? Both
MS-Test and the newest version of xUnit.net now support the idea of a unit test
that is async, and can therefore use the await keyword to block on a call that
returns a Task.</p>

<!-- more -->

<p>One of the interesting things about this occurs when you use async with a faked
interface that contains an async method.  Consider the case where you have an
interface that returns <code>Task&lt;string&gt;</code> because it is expected that some or all
of the implementors could be long running. Your interface definition might look
like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">public</span> <span class="k">interface</span> <span class="n">IDoStuff</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="n">Task</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;</span> <span class="n">LongRunningOperation</span><span class="p">();</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>When you are testing a class that consumes this interface, you will want to
provide a fake implementation of that method. So what is the best way to do
that?</p>

<p>(Note: I will use VS11 Fakes for this example but it really doesn&rsquo;t matter.)</p>

<p>You might write a test like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="n">async</span> <span class="n">Task</span> <span class="nf">TestingWithRealRunningTasks</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">stub</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIDoStuff</span>
</span><span class="line">      <span class="p">{</span>
</span><span class="line">         <span class="n">LongRunningOperation</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Task</span><span class="p">.</span><span class="n">Run</span><span class="p">(</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="s">&quot;Hello there&quot;</span> <span class="p">);</span>
</span><span class="line">      <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">SystemUnderTest</span><span class="p">(</span><span class="n">stub</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">await</span> <span class="n">sut</span><span class="p">.</span><span class="n">DoSomething</span><span class="p">();</span> <span class="c1">// This calls the interface stub</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span> <span class="s">&quot;Interface said &#39;Hello there&#39;&quot;</span><span class="p">,</span> <span class="n">result</span> <span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Assuming <code>DoSomething()</code> produces the formatted string that is expected, this
test will work. But there&rsquo;s a bit that is unfortunate&hellip;</p>

<p>You actually did spin off a background thread when you called Task.Run().  You
can confirm this with some well-placed breakpoints and looking at the threads.</p>

<p>But did you need to do that in your fake object? Not really. It probably slowed
your test down by a bit and really isn&rsquo;t required.</p>

<p>The <code>System.Threading.Tasks</code> namespace includes a class you can use to help you
with these kinds of things: <code>TaskCompletionSource&lt;T&gt;</code> (see <a href="http://msdn.microsoft.com/en-us/library/dd449174.aspx">MSDN Docs</a>). This very cool
class can be used for a lot of different things like making event-based async
live in a TAP world, etc.</p>

<p>Stephen Toub says a lot about TCS in his post <a href="http://blogs.msdn.com/b/pfxteam/archive/2009/06/02/9685804.aspx">The Nature of TaskCompletionSource&lt;TResult&gt;</a>
but the part most relevant to us here is where he says:</p>

<blockquote><p>Unlike Tasks created by Task.Factory.StartNew, the Task handed out by<br />TaskCompletionSource&lt;TResult&gt; does not have any scheduled delegate associated<br />with it. Rather, TaskCompletionSource&lt;TResult&gt; provides methods that allow you<br />as the developer to control the lifetime and completion of the associated Task.<br />This includes SetResult, SetException, and SetCanceled, as well as TrySet<br />variants of each of those.</p></blockquote>

<p>It &ldquo;does not have any schedule delegates associated with it.&rdquo; That sounds
perfect!</p>

<p><strong><em>UPDATE: This code is only required in the .NET 4.0 version of Task
Parallels. See below for an updated .NET 4.5 version of this test that
doesn&rsquo;t require my helper method.</em></strong></p>

<p>So what I&rsquo;m going to do is use TCS to create a task that just contains the
concrete return value, acting as if the long running operation has happened,
and returning a Task that the consuming code can treat normally.</p>

<p>Rewriting that last test using a TCS it would look like this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="n">async</span> <span class="n">Task</span> <span class="nf">TestWithHandCodedTCS</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">stub</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIDoStuff</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">      <span class="n">LongRunningOperation</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span><span class="line">         <span class="n">var</span> <span class="n">tcs</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TaskCompletionSource</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;();</span>
</span><span class="line">         <span class="n">tcs</span><span class="p">.</span><span class="n">SetResult</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">);</span>
</span><span class="line">         <span class="k">return</span> <span class="n">tcs</span><span class="p">.</span><span class="n">Task</span><span class="p">;</span>
</span><span class="line">      <span class="p">}</span>
</span><span class="line">   <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">SystemUnderTest</span><span class="p">(</span><span class="n">stub</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">await</span> <span class="n">sut</span><span class="p">.</span><span class="n">DoSomething</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">&quot;Interface said &#39;Hello there!&#39;&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Now I no longer have the background thread! But that chunk of code where I
create the TCS is annoying so I can refactor it out into a reusable helper
method:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="k">internal</span> <span class="k">class</span> <span class="nc">TaskHelpers</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">    <span class="k">public</span> <span class="k">static</span> <span class="n">Task</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span> <span class="n">CreatePseudoTask</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;(</span><span class="n">T</span> <span class="n">result</span><span class="p">)</span>
</span><span class="line">    <span class="p">{</span>
</span><span class="line">        <span class="n">TaskCompletionSource</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;</span> <span class="n">tcs</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TaskCompletionSource</span><span class="p">&lt;</span><span class="n">T</span><span class="p">&gt;();</span>
</span><span class="line">        <span class="n">tcs</span><span class="p">.</span><span class="n">SetResult</span><span class="p">(</span><span class="n">result</span><span class="p">);</span>
</span><span class="line">        <span class="k">return</span> <span class="n">tcs</span><span class="p">.</span><span class="n">Task</span><span class="p">;</span>
</span><span class="line">    <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Now I can rewrite the test to this:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="n">async</span> <span class="n">Task</span> <span class="nf">TestWithPseudoTask</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">stub</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIDoStuff</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">       <span class="n">LongRunningOperation</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="n">TaskHelpers</span><span class="p">.</span><span class="n">CreatePseudoTask</span><span class="p">&lt;</span><span class="kt">string</span><span class="p">&gt;(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">)</span>
</span><span class="line">   <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">SystemUnderTest</span><span class="p">(</span><span class="n">stub</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">await</span> <span class="n">sut</span><span class="p">.</span><span class="n">DoSomething</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">&quot;Interface said &#39;Hello there!&#39;&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Nice and simple, and easy to read, without all the mess of creating real
scheduled background Task delegates.</p>

<p>What do you think? Useful? I&rsquo;ve found it to help me a bit when writing tests
against async stuff.</p>

<hr />

<p><strong><em>UPDATE: Using Task.FromResult in .NET 4.5</em></strong></p>

<p>Apparently this patterns was common enough in the .NET 4.0 version of TPL that
the team decided to just &ldquo;make it so&rdquo; and bake it in so we don&rsquo;t need the
helper method anymore. And since it is baked in, it is probably optimized to
perform even better.</p>

<p>Here is the updated test using the new <code>Task.FromResult</code> method.</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
</pre></td><td class="code"><pre><code class="csharp"><span class="line"><span class="na">[TestMethod]</span>
</span><span class="line"><span class="k">public</span> <span class="n">async</span> <span class="n">Task</span> <span class="nf">TestWithFromResultHelper</span><span class="p">()</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line">   <span class="c1">// Arrange</span>
</span><span class="line">   <span class="n">var</span> <span class="n">stub</span> <span class="p">=</span> <span class="k">new</span> <span class="n">StubIDoStuff</span>
</span><span class="line">   <span class="p">{</span>
</span><span class="line">       <span class="n">LongRunningOperation</span> <span class="p">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="n">Task</span><span class="p">.</span><span class="n">FromResult</span><span class="p">(</span><span class="s">&quot;Hello there!&quot;</span><span class="p">)</span>
</span><span class="line">   <span class="p">};</span>
</span><span class="line">   <span class="n">var</span> <span class="n">sut</span> <span class="p">=</span> <span class="k">new</span> <span class="n">SystemUnderTest</span><span class="p">(</span><span class="n">stub</span><span class="p">);</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Act</span>
</span><span class="line">   <span class="n">var</span> <span class="n">result</span> <span class="p">=</span> <span class="n">await</span> <span class="n">sut</span><span class="p">.</span><span class="n">DoSomething</span><span class="p">();</span>
</span><span class="line">
</span><span class="line">   <span class="c1">// Assert</span>
</span><span class="line">   <span class="n">Assert</span><span class="p">.</span><span class="n">AreEqual</span><span class="p">(</span><span class="s">&quot;Interface said &#39;Hello there!&#39;&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>Enjoy!</p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Editing Octopress/Jekyll posts in Vim]]></title>
    <link href="http://www.peterprovost.org//blog/2012/04/22/editing-octopress-slash-jekyll-posts-in-vim/"/>
    <updated>2012-04-22T02:26:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/04/22/editing-octopress-slash-jekyll-posts-in-vim</id>
    <content type="html"><![CDATA[<p><strong><em>Update</em></strong> - I was able to get backtick code blocks working much
better, and made a stab at the YAML Front Matter, but it doesn&rsquo;t seem to work
using <code>syntax include</code>. See the <a href="https://github.com/PProvost/vim-markdown-jekyll">git repo</a> for the updated source.</p>

<p>I use <a href="http://www.vim.org/">Vim</a> as my day-to-day, non-IDE text editor. Yeah, I know everyone is in
love with Notepad2, Notepad+ or whatever the new favorite on the block is. I&rsquo;ve
been a vi/vim guy for ages and am not gonna change.</p>

<p>Since switching my blog to <a href="http://octopress.org/">Octopress</a>, I&rsquo;ve been writing all my posts in Vim.
Vim does a nice job with <a href="http://daringfireball.net/projects/markdown/">Markdown</a>, but it doesn&rsquo;t know anything about the other
things that are often used in a <a href="https://github.com/mojombo/jekyll">Jekyll</a> markdown file.</p>

<p>The two big things are:</p>

<ol>
  <li>It gets confused by the <a href="https://github.com/mojombo/jekyll/wiki/yaml-front-matter">YAML Front Matter</a></li>
  <li>It can go nuts over some of the Liquid filters and tags</li>
</ol>

<p>Fortunately Vim has a nice way of letting you add new things to an existing
syntax definition. You just create another syntax file and put it in the
<code>after</code> directory in your <code>~/.vim</code> directory. Then you just add the new syntax
descriptors and restart Vim.</p>

<!-- more -->

<p>For the first problem, I found a <a href="http://www.codeography.com/2010/02/20/making-vim-play-nice-with-jekylls-yaml-front-matter.html">blog post by Christopher Sexton</a> that had a
nice regex match for the YAML Front Matter. He has it included in his
<a href="https://github.com/csexton/jekyll.vim">Jekyll.vim plugin</a> (which I don&rsquo;t use, but it is pretty cool).</p>

<p>A quick catch-all regex for Liquid tags and another for backtick code blocks
and it works pretty damn well.</p>

<p>Here&rsquo;s the code:</p>

<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span>markdown.vim </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
</pre></td><td class="code"><pre><code class="vim"><span class="line"><span class="c">    &quot; YAML front matter</span>
</span><span class="line">    <span class="nb">syntax</span> <span class="k">match</span> Comment <span class="sr">/\%^---\_.\{-}---$/</span> contains<span class="p">=</span>@Spell
</span><span class="line"><span class="c">    </span>
</span><span class="line"><span class="c">    &quot; Match Liquid Tags and Filters</span>
</span><span class="line">    <span class="nb">syntax</span> <span class="k">match</span> Statement <span class="sr">/{[%{].*[}%]}/</span>
</span><span class="line"><span class="c">    </span>
</span><span class="line"><span class="c">    &quot; Match the Octopress Backtick Code Block line</span>
</span><span class="line">    <span class="nb">syntax</span> <span class="k">match</span> Statement <span class="sr">/^```.*$/</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>

<p>I do think it would be cool if I could do a few other things:</p>

<ol>
  <li>Actually use YAML syntax coloring in the Front Matter. I&rsquo;d like not to have
to reimplement the YAML syntax to accomplish this, but from looking at the
way the HTML syntax works for javascript, I may have to.</li>
  <li>Build in understanding of the <a href="http://octopress.org/docs/plugins/codeblock/">Octopress codeblock tag</a> and disable
Markdown syntax processing within it.</li>
</ol>

<p>It also has a three line ftplugin tweak to force markdown files to use
expandtab and use a 3 character tabstop. Since I typically keep tabs (I don&rsquo;t
have expandtab in my vimrc) and since Markdown actually uses the spaces to mean
things, this just works better. If you don&rsquo;t like that part, just delete the
file.</p>

<p>If you want to use it, I recommend using pathogen and then clone the <a href="https://github.com/PProvost/vim-markdown-jekyll">GitHub
repository</a> into your bundle folder. </p>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rules of the Road (Redux)]]></title>
    <link href="http://www.peterprovost.org//blog/2012/04/21/rules-of-the-road-redux/"/>
    <updated>2012-04-21T23:05:00+00:00</updated>
    <id>http://www.peterprovost.org//blog/2012/04/21/rules-of-the-road-redux</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.peterprovost.org//images/blog/2012-04-21-rules-of-the-road-redux/share-the-road.jpg" width="300" height="402" /></p>

<p>It is so much fun to go back and look at old posts. I saw Scott Hanselman
mention on Twitter that he&rsquo;d recently marked his 10th anniversary as a blogger.
Since I just converted all my posts from SubText to Markdown, I&rsquo;ve been going
through the older ones. Sometimes I find one and say &ldquo;Ugh, did I really say
that?&rdquo; but other times I find a good one and it still resonates with me as much
as it did when I originally wrote it.</p>

<p>This is about one of those good ones.</p>

<p>The post I found was from January 29, 2009 and was called <a href="http://www.peterprovost.org/blog/2009/01/29/Rules-of-the-Road/">Rules of the
Road</a>.  In that post I talked about how I found these stashed away in a
OneNote file from 2006, so these have been with me for a while.</p>

<!-- more -->

<p>When I was growing up, my dad occasionally had issues with an ulcer in his
duodenum. It was stress related. He was a very Type-A kind of person, in an
occasionally stressful job, with all the stresses that one expects starting out
a family. (You know&hellip; money, kids, etc.)</p>

<p>It was during this time that he started using three rules to help him deal with
it. He told me those rules as I became older and was in college, where there
can be similar but different stresses. I didn&rsquo;t really take them to heart
though until I became a manager and really started to experience stress.</p>

<p>Since then I&rsquo;ve added some more to his list.</p>

<h2 id="the-rules">The Rules</h2>

<ol>
  <li>Don&rsquo;t stress out about things you can&rsquo;t control - ignore them</li>
  <li>Don&rsquo;t stress out about things you can control - fix them</li>
  <li>If you have an issue with someone (anyone), talk to them about it
immediately, do not let it fester</li>
  <li>Help people who politely and sincerely ask for help</li>
  <li>Fight for what you believe in</li>
  <li>Admit when you are wrong and don&rsquo;t be afraid to apologize</li>
  <li>Reserve the right to change your mind</li>
  <li>You do not have to justify saying no to someone</li>
</ol>

<h2 id="lets-look-at-each-one">Let&rsquo;s look at each one</h2>

<blockquote><p>1. Don't stress out about things you can't control - ignore them  <br />2. Don't stress out about things you can control - fix them</p></blockquote>

<p>The first two of my dad&rsquo;s original list are really the most important of the
bunch. They help you manage and control your own reactions and expectations,
and can play a huge part in increasing your personal happiness. </p>

<p>It is amazing how often people will stress out and complain about things over
which they have no control. It is even more amazing how often people whine and
complain about things they <strong><em>can</em></strong> control. This is related to the old adage,
&ldquo;Change your environment or change your environment.&rdquo; Don&rsquo;t like your job?  Fix
it or quit. You are in control over that. The same attitude can be applied to
just about anything that stresses you.</p>

<blockquote><p>3. If you have an issue with someone (anyone), talk to them about it<br />immediately, do not let it fester</p></blockquote>

<p>Rule #3 is really just a special case of Rule #2, but pertaining to people. But
this special case is important because we often forget that our relationships
and communications with our peers, colleagues, friends and family are in our
control. If you have an issue with something someone said or did, burying that
down in your subconscious will only make it worse. These conversations can be
hard, but they are essential to maintaining good relationships with people.</p>

<blockquote><p>4. Help people who politely and sincerely ask for help.</p></blockquote>

<p>This one is obvious to me, but particularly important on teams. Your family,
friends and team should take priority over almost everything else. And when one
of them comes to you with a request for help, help them.</p>

<blockquote><p>5. Fight for what you believe in</p></blockquote>

<p>This is another special case of #2 and it another example of &ldquo;Change your
environment or change your environment.&rdquo; If you believe strongly that something
needs to change, don&rsquo;t leave it unchallenged. This is true at work and at home.
But watch out for things that are beyond your control (Rule #1). If they really
are beyond your control, then you probably need to stop caring about it so
much.</p>

<blockquote><p>6. Admit when you are wrong and don't be afraid to apologize.</p></blockquote>

<p>I&rsquo;m always amazed how many people believe that admitting a mistake and
apologizing are somehow a sign of weakness. Nothing could be farther from the
truth. True self-confidence and personal strength is demonstrated when you can
openly and sincerely admit you made a mistake and apologize for it. It is
amazing how well this works with people and how it can help establish trust and
respect.</p>

<blockquote><p>7. Reserve the right to change your mind</p></blockquote>

<p>I always think of this one during election year because it seems that our
political system doesn&rsquo;t agree with it. How many times have you seen one
politician accuse another of &ldquo;flip flopping&rdquo;?</p>

<p>I saw a good quote on Twitter the other day that reminded me of this rule:</p>

<blockquote><p>Teaching kids to accept baseless assertions is damaging.  <br />Teaching them to ignore contradictory evidence is downright dangerous.</p><footer><strong>Anonymous, Twitter</strong> <cite><a href="http://www.twitter.com/AtheistQOTD/status/193851875085398018">www.twitter.com/AtheistQOTD/&hellip;</a></cite></footer></blockquote>

<p>Anyone who dogmatically sticks to a position because they&rsquo;re unwilling to
change is someone I simply cannot respect or work with. Everyone has to be
willing to listen to evidence and when presented with new information you have
to be willing to change your mind. Of course you don&rsquo;t <em>have</em> to change your
mind, it is about the willingness to accept new evidence and adjust your
position. Anything else is simply illogical and foolish.</p>

<p>When you know better, do better.</p>

<blockquote><p>8. You do not have to justify saying no to someone</p></blockquote>

<p>Many people, when presented with a request they are unable or unwilling to do
will seek to rationalize or explain away their reason. If you have a reason and
are willing to share it, go for it, but don&rsquo;t feel like you always have to
justify saying no to someone. Be polite and respectful, but there is nothing
wrong with simply saying, &ldquo;No I&rsquo;m sorry, I can&rsquo;t do that.&rdquo;</p>

<h2 id="conclusion">Conclusion</h2>

<p>I&rsquo;m not sure if this will help you but these are rules that I use in my life to
help guide my behavior. The essence is about being open and honest with
yourself and with those around you. Consistent application of them can help you
be happier, less stressed and be someone that people know they can trust and
respect.</p>

]]></content>
  </entry>
  
</feed>
