.Net, TDD

Stub HttpContext

Have you ever tried to TDD objects that use HttpContext, HttpResponse, or HttpRequest?  If you did, you probably created wrappers for these classes.  No more.  With .NET 3.5 Microsoft gave us System.Web.Abstractions.dll that extends System.Web namespace.  In this post I’d like to show an example how to stub HttpContext.

Here’s the system under test:

    public class MyContext
    {
        private readonly HttpContextBase _context;

        // This constructor is called by production system.
        public MyContext() : this(new HttpContextWrapper(HttpContext.Current))
        {}

        // Test calls this constructor
        public MyContext(HttpContextBase context)
        {
            _context = context;
        }

        public bool IsItemCached(string item)
        {
            return _context.Cache[item] != null;
        }
    }

Here are the unit tests:

    [TestFixture]
    public class MyContextTester
    {
        private HttpContextBase _contextStub;
        private MyContext _myContext;

        [SetUp]
        public void StartTest()
        {
            _contextStub = MockRepository.GenerateMock<HttpContextBase>();
            _myContext = new MyContext(_contextStub);
        }

        [Test]
        public void IsItemCached_if_Cache_item_is_null_return_false()
        {
            _contextStub.Stub(x => x.Cache).Return(HttpRuntime.Cache);
            Assert.IsFalse(_myContext.IsItemCached("NotThere"), "False is expected.");
        }

        [Test]
        public void IsItemCached_if_Cache_item_is_NOT_null_return_true()
        {
            HttpRuntime.Cache.Insert("I am HERE", "value");
            _contextStub.Stub(x => x.Cache).Return(HttpRuntime.Cache);
            Assert.IsTrue(_myContext.IsItemCached("I am HERE"), "True is expected.");
        }
    }

kick it on DotNetKicks.com

Advertisements
MbUnit

Fixture order with MbUnit

In my company we have some people who are using NUnit and some are using MbUnit. I’m personally a huge fan of MbUnit. As a company we made a decision to use MbUnit only. Someone at my company had a problem with MbUnit because it would run tests in random order. In his case he wanted to run TestFixtures in specific order. He took advantage that NUnit runs TestFixtures in alphabetical order and named the fixtures in order he wants to run them. For example:

A_Fixture
B_Fixture

N_Fixture

To help to solve his problem I reflected on MbUnit.Framework.dll, and found DependOnAttribute. And that exactly what he wanted.

Here how it works. Let assume that we created four.

A_Fixture
Bad_Fixture
B_Fixture
B_Child_Fixture

[TestFixture]
public class A_Fixture
{
[
Test]
public void Success()
{ }
}

[TestFixture]
public class Bad_Fixture
{
[
Test]
public void Failure()
{
Assert.IsTrue(false);
}

[Test]
public void Success()
{
}
}

[TestFixture]
[
DependsOn(typeof(A_Fixture))]
[
DependsOn(typeof(Bad_Fixture))]
public class B_Fixture
{
[
Test]
public void Success()
{
}
}

[TestFixture]
[
DependsOn(typeof(B_Fixture))]
public class B_Child_Fixture
{
[
Test]
public void Success()
{
}
}

You can see that B_Child_Fixture depends on B_Fixture. B_Fixture at the same time depends on A_Fixture and Bad_Fixture. As you can see one of the tests in Bad_Fixture will fail. As a result B_Fixture and B_Child will not run.

Here’s the result in the MbUnit GUI.