# 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;
        public virtual void Setup()
            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();
        public void TearDown()


    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;
    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);
        public void Will_display_error_message_if_logged_in()
            authServiceStub.Stub(c => c.IsAuthenticated()).Return(true);
            var result = controller.Index() as RedirectToRouteResult;
            var message = controller.TempData["Message"] as string;
            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  #   
# 01 Aralık 2007 Cumartesi
Just for the fun :)

Download: TDDOANG.pdf (734.42 KB)

  • Yeah. I know it's not complete / 100% accurate.
  • Yep. There are other tools, I know.
  • Yes, Alt.Net is not the authority. But it's the most active community in MS ecosystem right now where you can ask questions on TDD.
posted on 01 Aralık 2007 Cumartesi 11:29:37 UTC  #