24 July 2013

Testing The DefaultRoute with Moq and NUnit

Just want to share this script to show how easy to test the default route using NUnit and Moq.

using System.Web;
using System.Web.Routing;
using NUnit.Framework;
using Moq;

namespace eHayop.Web.Tests
{
    [TestFixture]
    public class RoutingTests
    {
        [Test]
        public void Route_Should_Have_A_Default_Action()
        {
            var routeCollection = new RouteCollection();
            RouteConfig.RegisterRoutes(routeCollection);

            var mock = new Mock<HttpContextBase>();
            mock.Setup(y => y.Request.AppRelativeCurrentExecutionFilePath).Returns("~/Animal/Index");
            RouteData routeData = routeCollection.GetRouteData(mock.Object);
            Assert.IsNotNull(routeData, "Got my route");
            Assert.AreEqual("Animal", routeData.Values["Controller"], "Returns a different controller");
            Assert.AreEqual("Index", routeData.Values["action"], "Returns a different action");
        }
    }
}


No comments:

Post a Comment