Unit testing a Umbraco RenderMvcController

Unit testing a Umbraco RenderMvcController

By | | Umbraco , Unit Testing

To unit test a Umbraco RenderMvcController, you need to grab the source code from github, compile the solution yourself, and get the Umbraco.Tests.dll and reference it on your test project.

In addition to that, you need to reference the SQLCE4Umbraco.dll which is distributed with the Umbraco packages, and Rhino.Mocks.dll which is internally for mocking.

To help you with this, I have compiled put the Umbraco.Tests.dll for Umbraco 6.1.5 and put it together with the Rhino.Mocks.dll and put it on this zip file.

Finally, derive your test from BaseRoutingTest, override the DatabaseTestBehavior to NoDatabasePerFixture, and get the UmbracoContext and HttpBaseContext by calling the GetRoutingContext method, as in the code below:

using System;
using Moq;
using NUnit.Framework;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core.Models;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;

namespace UnitTests.Controllers
{
    public class Entry
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public string Summary { get; set; }
        public string Content { get; set; }
        public string Author { get; set; }
        public string[] Tags { get; set; }
        public DateTime Date { get; set; }
    }

    public interface IBlogService
    {
        Entry GetBlogEntry(int id);
    }

    public class BlogEntryController : RenderMvcController
    {
        private readonly IBlogService _blogService;

        public BlogEntryController(IBlogService blogService, UmbracoContext ctx)
            : base(ctx)
        {
            _blogService = blogService;
        }

        public BlogEntryController(IBlogService blogService)
            : this(blogService, UmbracoContext.Current)
        {
        }

        public override ActionResult Index(RenderModel model)
        {
            var entry = _blogService.GetBlogEntry(model.Content.Id);

            // Test will fail if we return CurrentTemplate(model) as is expecting 
            // the action from ControllerContext.RouteData.Values["action"]
            return View("BlogEntry", entry);
        }
    }

    [TestFixture]
    public class RenderMvcControllerTests : BaseRoutingTest
    {
        protected override DatabaseBehavior DatabaseTestBehavior
        {
            get { return DatabaseBehavior.NoDatabasePerFixture; }
        }

        [Test]
        public void CanGetIndex()
        {
            const int id = 1234;
            var content = new Mock<IPublishedContent>();
            content.Setup(c => c.Id).Returns(id);
            var model = new RenderModel(content.Object, CultureInfo.InvariantCulture);
            var blogService = new Mock<IBlogService>();
            var entry = new Entry { Id = id };
            blogService.Setup(s => s.GetBlogEntry(id)).Returns(entry);
            var controller = GetBlogEntryController(blogService.Object);

            var result = (ViewResult)controller.Index(model);

            blogService.Verify(s => s.GetBlogEntry(id), Times.Once());
            Assert.IsNotNull(result);
            Assert.IsAssignableFrom<Entry>(result.Model);
        }

        private BlogEntryController GetBlogEntryController(IBlogService blogService)
        {
            var routingContext = GetRoutingContext("/test");
            var umbracoContext = routingContext.UmbracoContext;
            var contextBase = umbracoContext.HttpContext;
            var controller = new BlogEntryController(blogService, umbracoContext);
            controller.ControllerContext = new ControllerContext(contextBase, new RouteData(), controller);
            controller.Url = new UrlHelper(new RequestContext(contextBase, new RouteData()), new RouteCollection());
            return controller;
        }
    }
}

This code has only been tested in Umbraco 6.1.5.

There are also a couple of threads you might want to read:

About us

We are based in Barcelona and specialize in medium and large Umbraco projects. We offer our experience, method and commitment to achieve the success of every project. We have been an official Umbraco partner since 2011.

All Categories