Tuesday, November 04, 2008
Update: Check the comment by Matt Hinze. It appears that we don't need this kind of complexity after Preview 4. Check this and this, for example. Thanks Matt. My lesson? When you're using pre-release software with lots of changes and improvements in each release, be sure to reevaluate all your past assumptions in each.

--

Just a note. I use a base like this code to avoid repetitive test setup when testing MVC controllers.

    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using MbUnit.Framework;
    using Rhino.Commons;
    using Rhino.Mocks;
 
    public abstract class ControllerTestBase<TController> 
        where TController : Controller
    {
        protected TController controller;
        protected ControllerContext controllerContext;
        protected IDisposable disposeGlobalUnitOfWorkRegistration;
        protected HttpContextBase httpContextStub;
        protected HttpSessionStateBase httpSessionStub;
        protected IUnitOfWork unitOfWorkStub;
 
        [SetUp]
        public virtual void Setup()
        {
            SetupController();
 
            unitOfWorkStub = MockRepository.GenerateStub<IUnitOfWork>();
            httpContextStub = MockRepository.GenerateStub<HttpContextBase>();
            httpSessionStub = MockRepository.GenerateStub<HttpSessionStateBase>();
            httpContextStub.Stub(x => x.Session).Return(httpSessionStub);
            var httpRequestStub = MockRepository.GenerateStub<HttpRequestBase>();
            httpContextStub.Stub(x => x.Request).Return(httpRequestStub);
            controllerContext = new ControllerContext(httpContextStub, new RouteData(), controller);
            controller.ControllerContext = controllerContext;
            disposeGlobalUnitOfWorkRegistration = UnitOfWork.RegisterGlobalUnitOfWork(unitOfWorkStub);
        }
 
        protected abstract void SetupController();
 
        [TearDown]
        public void TearDown()
        {
            disposeGlobalUnitOfWorkRegistration.Dispose();
        }
    }


Then...


    using System.Web.Mvc;
    using Common;
    using Core.Authentication;
    using Core.Repositories;
    using Domain.Entities;
    using MbUnit.Framework;
    using Rhino.Commons;
    using Rhino.Mocks;
    using UI.Controllers;
    using UI.Properties;
 
    [TestFixture]
    public class RegisterControllerTests : ControllerTestBase<RegisterController>
    {
        private IRepository<User> repositoryStub;
        private IAuthenticationService authServiceStub;
 
        protected override void SetupController()
        {
            repositoryStub = MockRepository.GenerateStub<UserRepository>();
            authServiceStub = MockRepository.GenerateStub<IAuthenticationService>();
            controller = new RegisterController(repositoryStub, authServiceStub);
        }
 
        [Test]
        public void Will_display_error_message_if_logged_in()
        {
            authServiceStub.Stub(c => c.IsAuthenticated()).Return(true);
            var result = controller.Index() as RedirectToRouteResult;
            Assert.IsNotNull(result);
            var message = controller.TempData["Message"] as string;
            Assert.IsFalse(string.IsNullOrEmpty(message));
            Assert.AreEqual(GlobalResources.CannotRegisterAlreadyRegistered, message);
            Assert.AreEqual(result.Values["action"], "Index");
            Assert.AreEqual(result.Values["controller"], "Message");
        }
    }
11/4/2008 9:11:05 PM UTC
Ouch! That ControllerContext stuff is terrible! It's a shame you have to go through all that just to test one action on the controller.

Sure, it's better than WebForms (which allowed ZILCH testing), but it's still not great :(
11/5/2008 7:34:12 AM UTC
@Chad,

Less friction, at least :) I'll check DoveTail (is that the name?) code if it's available. Jeremy mentioned it as a possible open framework and I'm eagerly waiting for that to happen.
11/5/2008 6:57:54 PM UTC
What version are you using? You don't need to do all that stuff in Preview 4 and later...
11/6/2008 9:05:15 AM UTC
That's what happens when you rely on prerelease technology but don't reevaluate all your decisions with each subsequent release :) A quick search shows that this approach is quite outdated. thanks for the pointer.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):