concept MvcContrib in category asp.net

appears as: MvcContrib, MvcContrib
ASP.NET MVC 2 in Action

This is an excerpt from Manning's book ASP.NET MVC 2 in Action.

When compared with the rest of the ASP.NET MVC Framework, testing routes isn’t easy or intuitive because a number of abstract classes need to be mocked out before route testing is possible. Luckily, MvcContrib has a nice fluent route-testing API that we can use to make testing these routes easier.

Luckily, we don’t have to deal with this if we’re smart. MvcContrib’s fluent route-testing API makes everything a lot easier. Listing 16.11 is the same test, using MvcContrib.

Listing 16.11. Cleaner route testing with MvcContrib’s TestHelper project

This is all done with the magic and power of extension methods and lambda expressions. Inside MvcContrib there’s an extension method on the string class that builds up a RouteData instance based on the parameters in the URL. The RouteData class has an extension method to assert that the route values match a controller and action .

Listing 22.8. A message handler class
using MvcContrib.PortableAreas; using RssWidgetPortableArea.Controllers; namespace RssWidgetPortableArea { public class RssMessageHandler : MessageHandler<RssWidgetRenderedMessage> { public override void Handle( RssWidgetRenderedMessage message) { //log the message to the application's log. } } }
ASP.NET MVC in Action with MvcContrib, NHibernate, and More

This is an excerpt from Manning's book ASP.NET MVC in Action with MvcContrib, NHibernate, and More.

Although the starter kits are quite basic, you should also check out more complete starter kits like those found at http://CommunityForMvc.net. This site contains a bare-bones template as well as one complete with MvcContrib, StructureMap, NHibernate, NUnit, NAnt, AutoMapper, Tarantino, Naak, NBehave, Rhino Mocks, WatiN, Gallio, Castle, 7zip, and more.

If you are coming from Ruby on Rails and are smitten with its built-in REST support, you’ll be disappointed to find that ASP.NET MVC has no built-in support for REST. However, due to the extensibility provided by the framework, it’s not difficult to achieve a RESTful architecture. MvcContrib has an implementation called SimplyRestful that contains a usable REST implementation. Look it up if you are interested in REST.

When compared with the rest of the ASP.NET MVC Framework, testing routes is not easy or intuitive. Although ASP.NET MVC has advanced the functions interfaces and abstract base classes, many elements still must be mocked out before route testing is possible. Luckily, MvcContrib has a nice, fluent route testing API, which we can use to make testing these routes easier. But before we look at that, listing 5.15 demonstrates how you would test a route with NUnit and Rhino Mocks.

Listing 5.15. Testing routes can be a pain.
using System.Web; using System.Web.Routing; using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; using Rhino.Mocks; namespace CodeCampServerRoutes.Tests { [TestFixture] public class NaiveRouteTester { [Test] public void root_matches_conference_controller_and_current_action() { const string url = "~/"; var request = MockRepository.GenerateStub<HttpRequestBase>(); request.Stub(x => x.AppRelativeCurrentExecutionFilePath) .Return(url).Repeat.Any(); request.Stub(x => x.PathInfo) .Return(string.Empty).Repeat.Any(); var context = MockRepository.GenerateStub<HttpContextBase>(); context.Stub(x => x.Request).Return(request).Repeat.Any(); RouteTable.Routes.Clear(); MvcApplication.RegisterRoutes(RouteTable.Routes); var routeData = RouteTable.Routes.GetRouteData(context); Assert.That(routeData.Values["controller"], Is.EqualTo("Conference")); Assert.That(routeData.Values["action"], Is.EqualTo("Current")); } } }

If all of our route tests looked like that, nobody would even bother. Those specific stubs on HttpContextBase and HttpRequestBase were not lucky guesses. It took a peek inside of Reflector to find out exactly what to mock. This is not how a testable framework should behave! Luckily, we do not have to deal with this if we are smart. MvcContrib’s fluent route testing API makes this a lot easier. Listing 5.16 is the same test, using MvcContrib:

Listing 5.16. Much better route testing with MvcContrib’s TestHelper project
using System.Web.Routing; using CodeCampServerRoutes.Controllers; using MvcContrib.TestHelper; using NUnit.Framework; namespace CodeCampServerRoutes.Tests { [TestFixture] public class FluentRouteTester { [Test] public void root_matches_conference_controller_and_current_action() { MvcApplication.RegisterRoutes(RouteTable.Routes); "~/".ShouldMapTo<ConferenceController>(x => x.Current()); } } }