<?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>Stephen Lacy &#187; Testing</title>
	<atom:link href="http://www.lacy.ie/category/programming/testing/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lacy.ie</link>
	<description>Impressions as an ASP.Net Developer</description>
	<lastBuildDate>Mon, 05 Jul 2010 09:26:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to Mock the HttpContext in asp.net</title>
		<link>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net</link>
		<comments>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net#comments</comments>
		<pubDate>Tue, 13 Oct 2009 10:18:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[httpcontext]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.lacy.ie/?p=232</guid>
		<description><![CDATA[I find mocking the http context rather difficult, you can&#8217;t inherit from it and making a wrapper class is a lot of work. Thankfully someone came up with a solution, unfortunately the link they included with the code is dead and I can&#8217;t seem to remember where I got the code, if anyone finds out [...]


Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I find mocking the http context rather difficult, you can&#8217;t inherit from it and making a wrapper class is a lot of work. Thankfully someone came up with a solution, unfortunately the link they included with the code is dead and I can&#8217;t seem to remember where I got the code, if anyone finds out where it comes from please comment and I&#8217;ll add it to the post.</p>
<p><span id="more-232"></span></p>
<pre>using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using System.Web.SessionState;

public sealed class MockHttpContext
{
    // NOTE: This code is based on the following article:
    // http://righteousindignation.gotdns.org/blog/archive/2004/04/13/149.aspx
    private const string ContextKeyAspSession = "AspSession";
    private const string ThreadDataKeyAppPath = ".appPath";
    private const string ThreadDataKeyAppPathValue = "c:\\inetpub\\wwwroot\\webapp\\";
    private const string ThreadDataKeyAppVPath = ".appVPath";
    private const string ThreadDataKeyAppVPathValue = "/webapp";
    private const string WorkerRequestPage = "default.aspx";

    private HttpContext context = null;

    private MockHttpContext()
        : base()
    {
    }

    public MockHttpContext(bool isSecure)
        : this()
    {
        Thread.GetDomain().SetData(
            MockHttpContext.ThreadDataKeyAppPath, MockHttpContext.ThreadDataKeyAppPathValue);
        Thread.GetDomain().SetData(
            MockHttpContext.ThreadDataKeyAppVPath, MockHttpContext.ThreadDataKeyAppVPathValue);
        SimpleWorkerRequest request = new WorkerRequest(MockHttpContext.WorkerRequestPage,
            string.Empty, new StringWriter(), isSecure);
        this.context = new HttpContext(request);

        HttpSessionStateContainer container = new HttpSessionStateContainer(
            Guid.NewGuid().ToString("N"), new SessionStateItemCollection(), new HttpStaticObjectsCollection(),
            5, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false);

        HttpSessionState state = Activator.CreateInstance(
             typeof(HttpSessionState),
             BindingFlags.Public | BindingFlags.NonPublic |
             BindingFlags.Instance | BindingFlags.CreateInstance,
             null,
             new object[] { container }, CultureInfo.CurrentCulture) as HttpSessionState;
        this.context.Items[ContextKeyAspSession] = state;
    }

    public HttpContext Context
    {
        get
        {
            return this.context;
        }
    }

    private class WorkerRequest : SimpleWorkerRequest
    {
        private bool isSecure = false;

        public WorkerRequest(string page, string query, TextWriter output, bool isSecure)
            : base(page, query, output)
        {
            this.isSecure = isSecure;
        }

        public override bool IsSecure()
        {
            return this.isSecure;
        }
    }
}</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fhow-to-mock-the-httpcontext-in-asp-net&amp;linkname=How%20to%20Mock%20the%20HttpContext%20in%20asp.net"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Improve your testing</title>
		<link>http://www.lacy.ie/improve-your-testing</link>
		<comments>http://www.lacy.ie/improve-your-testing#comments</comments>
		<pubDate>Thu, 10 Sep 2009 09:46:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[Integration Testing]]></category>
		<category><![CDATA[The Art of Unit Testing]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://lacy.ie/?p=218</guid>
		<description><![CDATA[Last night I was at the first meeting of the local alt.net book club, we&#8217;re currently going through a book called the art of unit testing. What I took from reading the first chapter and the discussion we had at the meeting was that you test your code already, your tests can be improved and unit testing [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.manning.com/osherove/"><img class="alignright size-thumbnail wp-image-219" title="The Art of Unit Testing" src="http://lacy.ie/wp-content/uploads/2009/09/osherove_cover150-150x150.jpg" alt="The Art of Unit Testing" width="150" height="150" /></a></p>
<p>Last night I was at the first meeting of the <a href="http://dublinalt.net">local alt.net</a> book club, we&#8217;re currently going through a book called the art of unit testing. What I took from reading the first chapter and the discussion we had at the meeting was that you test your code already, your tests can be improved and unit testing will improve it. My first mistake was to confuse what unit testing was. I thought it was just automated testing. I had used a unit testing framework and that&#8217;s all that I had learnt from doing so. However once you start reading up on the best practices of testing you hear a different story.</p>
<p><span id="more-218"></span>Unit testing is testing a piece of logic as a unit isolated from all dependancies. That&#8217;s not the offical definition, it&#8217;s only designed to cover the bit that I msised. When you unit test a piece of code, you shouldn&#8217;t call other parts of your code. If necessary you should fake, mock or stub the rest of your logic in order to isolate that code properly. When you get the result of that test, you&#8217;ll then know whether that unit of logic is working or not.</p>
<p>Alternatively you could all it to call other units of logic and then you&#8217;ll know either that all is well or something somewhere in your code is broken. Which is fine if you have a small project that will never grow, because then you&#8217;ll only lose a small amount of time hunting down bugs gradually increasing as your grasp of the project slips over time. However if you have a large project you could easily lose days, it&#8217;s even worse when you have a whole team full of people that every time there is a failed test, the whole project doesn&#8217;t work and no one can proceed.</p>
<p>That&#8217;s what I took away from it, I&#8217;d recommend book clubs as a pain free way to get through technical books and I shall be using unit testing next chance I get.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fimprove-your-testing&amp;linkname=Improve%20your%20testing"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/improve-your-testing/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->