<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Trevor Power &#187; Mocking</title>
	<atom:link href="http://blog.trevorpower.com/index.php/tag/mocking/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.trevorpower.com</link>
	<description>Software development and other thoughts</description>
	<lastBuildDate>Fri, 20 Aug 2010 07:33:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Not mocking the HttpContext?</title>
		<link>http://blog.trevorpower.com/index.php/not-mocking-the-httpcontext/</link>
		<comments>http://blog.trevorpower.com/index.php/not-mocking-the-httpcontext/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 20:42:00 +0000</pubDate>
		<dc:creator>trevorpower</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://trevorpower.com/blog/?p=28</guid>
		<description><![CDATA[Someone asked me a question recently about mocking, and I had to say I didn&#8217;t have much experience with it. I found this answer a bit odd myself as I&#8217;ve been doing a lot of TDD with an ASP.Net MVC application. I&#8217;d seen lots of people talking about mocking the HttpContext and wondered &#8220;Why haven&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fblog.trevorpower.com%2Findex.php%2Fnot-mocking-the-httpcontext%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fblog.trevorpower.com%2Findex.php%2Fnot-mocking-the-httpcontext%2F" height="61" width="51" /></a></div><p>Someone asked me a question recently about mocking, and I had to say I didn&#8217;t have much experience with it. I found this answer a bit odd myself as I&#8217;ve been doing a lot of TDD with an ASP.Net MVC application. I&#8217;d seen lots of people talking about mocking the HttpContext and wondered &#8220;Why haven&#8217;t<span class="Apple-style-span" style="font-weight: bold;"> <span class="Apple-style-span" style="font-weight: normal;">I</span></span> had to do this?&#8221;.
<div></div>
<div>The first time I started TDD, I was working on converting an existing ASP.Net application to ASP.Net MVC. Most of my controller actions checked the current security principle in the context, so I immediately ran into problems when trying to test these.</div>
<div></div>
<div>I didn&#8217;t know much about MVC or TDD so used what I did know. I knew about a powerful component of MVC called an action invoker, that could be overridden to handle some parameter stuff. I found that I could easily create an ActionInvoker that would populate parameters of a certain type (namely IPrincipal) using the following code:</div>
<pre name="code" class="c-sharp:nocontrols:nogutter" cols="80" rows="10">
public class ActionInvoker : ControllerActionInvoker
{
   protected override object GetParameterValue(System.Reflection.ParameterInfo parameterInfo)
   {
      if (parameterInfo.ParameterType == typeof(IPrincipal))
         return HttpContext.Current.User;

      return base.GetParameterValue(parameterInfo);
   }
}
</pre>
<p>
<div>And put the following into my controller constructors:</div>
<pre name="code" class="c-sharp:nocontrols:nogutter" cols="80" rows="10">
public MyController()
{
   ActionInvoker = new ActionInvokers.ActionInvoker();
}
</pre>
<div>This made it easy to test my controller actions. I just needed to mock a simple IPrincipal. I used a similar solution when my file upload controller needed access to HttpContext.Current.Request.Files.</div>
<div></div>
<div>At the moment my code only depends on a few simple objects that come from the HttpContext and not the context itself. Surely this is better, if not please comment. I&#8217;m going to change the above to conform with everyone else by mocking the HttpContext but the only reason I see for doing this, is that the above action invoker is not testable. Is this the only reason?</div>
<div></div>
<div>Before you point it out to me, I have recently moved most of my security code into AuthorizeAttributes. Now my unit tests for invalid calls (security wise) to controller actions all fail but that&#8217;s for a future post.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.trevorpower.com/index.php/not-mocking-the-httpcontext/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
