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

Antonio wrote re: Testing URL Generation on Routes in MVC Framework
on 09-22-2010 1:23 PM

My dick twitched again in answer. Like a zombie, I moved to the bottom of the lounge, and stroked my dick, as I looked at the brown bud I had always been so fascinated with before. I was finally going to stick my pole in a woman's butt. I moved toward her, clambered over her, and pushed up to my knees. My dick was a few inches from her pussy and ass bud. She reached down, got the lube and squirted some in her hand. The she sat up, and began stroking my steel rod with her soft, slick hands. That was heaven itself. After I was completely covered in the lube, she pulled her legs back over her head and said "Take my ass, go slow, you are too thick to just ram it in, but if you go slowly, my sphincter will stretch and you can get it all the way in." My dick was pulsing with my heart as I inched forward, the tip nearing her puckered ring.

"Doesn't Gregory feel your tits like this, or any other way?"

"Doesn't Gregory feel your tits like this, or any other way?"

"That is so sexy. Did you both cum?"

It was Sunday night before she plucked up the courage to tell Adam about Tuesday's meeting. He wasn't happy � but by then, they had talked their way through the pain that Adam had suffered while watching his wife in the arms of another man. Sunday night was also the first time since she had returned home from Robert that Adam had managed to get his cock inside his wife's cunt � they had one hell of a fuck.

hrothnerta wrote re: Testing URL Generation on Routes in MVC Framework
on 09-30-2010 3:17 AM

emission assessment year

devlicio.us wrote re: Testing URL Generation on Routes in MVC Framework
on 05-12-2011 5:05 PM

Testing url generation on routes in mvc framework.. May I repost it? :)

devlicio.us wrote re: Testing URL Generation on Routes in MVC Framework
on 06-03-2011 4:35 AM

Testing url generation on routes in mvc framework.. Peachy :)

devlicio.us wrote re: Testing URL Generation on Routes in MVC Framework
on 06-05-2011 4:52 AM

Testing url generation on routes in mvc framework.. Nice :)

devlicio.us wrote re: Testing URL Generation on Routes in MVC Framework
on 06-26-2011 8:31 PM

Testing url generation on routes in mvc framework.. Keen :)

a1dx porno 5e7q wrote re: Testing URL Generation on Routes in MVC Framework
on 07-02-2011 10:41 AM

Testing url generation on routes in mvc framework.. Bully :)

cq5o sex video iiut wrote re: Testing URL Generation on Routes in MVC Framework
on 07-02-2011 11:39 PM

Testing url generation on routes in mvc framework.. Dandy :)

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
Umbraco <-- NEW Friend!
NServiceBus <-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)