# 04 Kasım 2008 Salı
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");
        }
    }
posted on 04 Kasım 2008 Salı 20:32:02 UTC  #   
Related posts:
Using Castle Validators with ASP.NET MVC Model Binders for Automatic Validation
TDD Poster - IKEA Style