Tim Barcz

Sponsors

The Lounge

Wicked Cool Jobs

Groups and Affiliations

Syndication

News

Testing URL Generation on Routes in MVC Framework

Today I wrote some code to output URLs in a certain manner in an ASP.NET MVC project.  The code I wrote was extremely simple, but I struggled, I mean S-T-R-U-G-G-L-E-D, to write tests against the new code and ensure correctness. I could easily very visually that it was working but the amount of setup and kludge around ViewContext, RequestContext and ControllerContext was causing me to pull my hair out.

I tweeted my frustrations as I packed up and left work for the day and got the following responses soon thereafter:

@TimBarcz outbound routing is still hard, sadly (from @subdigital)

@TimBarcz …a way to test #aspnetmvc a helper to reduce the ridiculous noise/setup would be helpful (from @ehexter)

Determined to not give up I pulled down the MVCContrib source and started to peck away. I’m working on committing and finalizing the code but here is a sneak peak of the changes.  Hopefully you agree that it is an improvement.

Before:

   1: [Test]
   2: public void Should_correctly_generate_session_url()
   3: {
   4:     new RouteConfigurator().RegisterRoutes();
   5:     var builder = new TestControllerBuilder();
   6:     var context = new RequestContext(builder.HttpContext, new RouteData());
   7:     context.HttpContext.Response.Expect(x => x.ApplyAppPathModifier(null)).IgnoreArguments().Do(new Func<string, string>(s => s)).Repeat.Any();
   8:     var urlhelper = new UrlHelper(context);
   9:     string url = urlhelper.RouteUrl("session", new { sessionKey = "this-is-the-session", conferenceKey = "austincodecamp" });
  10:     url.ShouldEqual("/austincodecamp/sessions/this-is-the-session");
  11: }

After:

   1: [Test]
   2: public void should_be_able_to_generate_url_from_named_route()
   3: {
   4:     OutBoundUrl.OfRouteNamed("session").ShouldMapToUrl("/sessions");
   5: }

And

   1: [Test]
   2: public void should_be_able_to_generate_url_from_controller_action()
   3: {
   4:     OutBoundUrl.Of<FunkyController>(x => x.New()).ShouldMapToUrl("/Funky/New");
   5: }

Much, much cleaner and easier on the eyes.

It’s not quite done yet and has some holes (accepting additional route values, etc) but it’s worthy of discussion and once completed should make one very frustrating aspect of testing in MVC even easier.


Posted 08-27-2009 12:15 AM by Tim Barcz

[Advertisement]

Comments

Richard Dingwall wrote re: Testing URL Generation on Routes in MVC Framework
on 08-27-2009 6:57 AM

Very cool! Any ETA on when we can get a play with this?

Tim Barcz wrote re: Testing URL Generation on Routes in MVC Framework
on 08-27-2009 8:12 AM

@Richard,

Shortly after I committed my code, Jeremy Skinner hit me up on Twitter saying he'll get the addition applied to MVCContrib as soon as possible.

Once there, if you download the trunk you'll have these bits.

progg.ru wrote Testing URL Generation on Routes in MVC Framework - Tim Barcz - Devlicio.us
on 08-27-2009 11:13 AM

Thank you for submitting this cool story - Trackback from progg.ru

Roberto wrote re: Testing URL Generation on Routes in MVC Framework
on 10-04-2009 4:27 AM

cool blog

Uligokithag wrote re: Testing URL Generation on Routes in MVC Framework
on 10-10-2009 11:34 AM

I bookmarked this link. Thank you for good job!,

Onythien wrote re: Testing URL Generation on Routes in MVC Framework
on 10-10-2009 10:27 PM

I want to say - thank you for this!,

Thaeveth wrote re: Testing URL Generation on Routes in MVC Framework
on 10-11-2009 2:13 AM

Great site. Keep doing.,

Agrireniel wrote re: Testing URL Generation on Routes in MVC Framework
on 10-11-2009 4:18 PM

Very interesting site. Hope it will always be alive!,

Onardossa wrote re: Testing URL Generation on Routes in MVC Framework
on 10-12-2009 3:04 AM

I want to say - thank you for this!,

Fiaviel wrote re: Testing URL Generation on Routes in MVC Framework
on 10-12-2009 6:47 AM

Great site. Keep doing.,

Ceabard wrote re: Testing URL Generation on Routes in MVC Framework
on 10-13-2009 11:55 AM

If you have to do it, you might as well do it right.,

Doctorset wrote re: Testing URL Generation on Routes in MVC Framework
on 11-20-2009 7:53 AM

This is the welcome page for the dentaldoctor.us Association web site.

Doctorset wrote re: Testing URL Generation on Routes in MVC Framework
on 11-20-2009 5:45 PM

This is the welcome page for the dentaldoctor.us Association web site.

Dietroly wrote re: Testing URL Generation on Routes in MVC Framework
on 11-20-2009 8:52 PM

This is the welcome page for the dietguidance.us Association web site.

Dietroly wrote re: Testing URL Generation on Routes in MVC Framework
on 11-21-2009 6:30 AM

This is the welcome page for the dietguidance.us Association web site.

Christmasmas wrote re: Testing URL Generation on Routes in MVC Framework
on 12-24-2009 5:55 PM

Can child christmas gift make parent can-child-christmas-gift-make-parent.christmasmas.com

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)

CodeBetter.Com