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 :)

social bookmarking seo wrote re: Testing URL Generation on Routes in MVC Framework
on 01-18-2013 10:27 PM

uI3LSt A big thank you for your blog post.Much thanks again. Awesome.

pills for weight loss wrote re: Testing URL Generation on Routes in MVC Framework
on 02-01-2013 5:15 AM

LjUBmv I really liked your blog article. Awesome.

buy viagra online wrote re: Testing URL Generation on Routes in MVC Framework
on 02-02-2013 10:28 PM

HAidKF I truly appreciate this blog article.Really looking forward to read more. Cool.

buy clomid no prescription wrote re: Testing URL Generation on Routes in MVC Framework
on 02-28-2013 3:47 AM

JrxZhz Really appreciate you sharing this article.Thanks Again. Great.

buy discount viagra wrote re: Testing URL Generation on Routes in MVC Framework
on 03-03-2013 5:09 PM

rnly7v Very informative article.Really thank you! Fantastic.

bookmarks wrote re: Testing URL Generation on Routes in MVC Framework
on 03-14-2013 2:22 AM

ri3HaS Awesome article post.Thanks Again. Really Great.

bookmaring service wrote re: Testing URL Generation on Routes in MVC Framework
on 03-15-2013 12:57 AM

g8zESy Very good blog article.Really thank you! Great.

Social bookmarks wrote re: Testing URL Generation on Routes in MVC Framework
on 03-24-2013 9:44 AM

2SYB16 Fantastic post. Cool.

social bookmarking service wrote re: Testing URL Generation on Routes in MVC Framework
on 04-03-2013 10:07 AM

8I0civ Muchos Gracias for your article post. Fantastic.

funny shirts wrote re: Testing URL Generation on Routes in MVC Framework
on 04-05-2013 4:15 PM

wow, awesome article. Really Cool.

social bookmarking service wrote re: Testing URL Generation on Routes in MVC Framework
on 04-07-2013 6:42 PM

Xx9vo6 I value the blog post.Much thanks again. Want more.

john wrote re: Testing URL Generation on Routes in MVC Framework
on 06-18-2013 3:45 AM
social bookmarks wrote re: Testing URL Generation on Routes in MVC Framework
on 06-19-2013 2:10 PM

dDYhto I really enjoy the article post.Much thanks again. Much obliged.

best social bookmarks wrote re: Testing URL Generation on Routes in MVC Framework
on 06-19-2013 2:10 PM

1TmgVa I really liked your article post. Really Great.

matt wrote re: Testing URL Generation on Routes in MVC Framework
on 06-20-2013 2:21 PM
super news that are fun wrote re: Testing URL Generation on Routes in MVC Framework
on 08-05-2013 2:11 AM

tG29ZY Hey, thanks for the blog.Much thanks again. Really Cool.

pro link building wrote re: Testing URL Generation on Routes in MVC Framework
on 09-24-2013 3:41 PM

fOB5u6 Thanks for the blog post.Much thanks again. Really Cool.

best linkbuilding wrote re: Testing URL Generation on Routes in MVC Framework
on 10-01-2013 7:53 AM

citmaR Major thankies for the blog.Really looking forward to read more. Great.

link building team wrote re: Testing URL Generation on Routes in MVC Framework
on 10-16-2013 10:28 AM

3krgmX Great article.Thanks Again. Keep writing.

top seo guys wrote re: Testing URL Generation on Routes in MVC Framework
on 10-26-2013 5:58 AM

KO5abu A big thank you for your blog.Really thank you! Really Great.

smashing top seo wrote re: Testing URL Generation on Routes in MVC Framework
on 10-30-2013 10:11 PM

dxfirq Muchos Gracias for your blog post.Really looking forward to read more. Really Cool.

link building wrote re: Testing URL Generation on Routes in MVC Framework
on 07-18-2014 3:50 AM

PmNe16 Thanks so much for the blog article.Much thanks again. Much obliged.

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
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)