Gray's Matter
Justice Gray - North America's favorite metrosexual strategy consultant
E-mail Justice
Subscribe to this feed
Subscribe to comments
Perennially Popular Posts
How to write a presentation abstract
My grandmother, globalization, and what's wrong with DateTime.Parse
A letter to my Worldvision sponsor child
Development Methodologies for the 21st Century
My grandmother & the difference between strong typing and weak typing
How I am becoming a better developer, part 1 of infinity
Facebook RSS Feeds: How to Write Your Own With C#
The Ten Keys To Happiness
My grandmother and the Global Assembly Cache (And Star Wars)
My grandmother and the difference between processes and threads
How to make a meeting request
Why you should never be an independent consultant
The DasBlog 1.9 AJAX Autosave True Story, Part 2
Other "metro" developers
Ben Scheirman
Bil Simser
D'Arcy Lussier
David Woods
Donald Belcham
Donald Jessop
George Clingerman
James Kovacs
Jason Hunt
Jason Row
Jean-Paul Boodhoo
Jeffrey Palermo
Joey deVilla
John Bristowe
Jonas Avellana
Kyle Baley
Mack Male
Martin Hinselwood
Michael McClenaghan
Mike Duncan
Mike Friesen
Mo Khan
Otyce
RoboSapient
Russell Ball
Scott Hanselman
Scott Reynolds
Steven Rockarts
Tech Embassy
Terry Thibodeau
Tom Opgenorth
Vladimir Levin
The Justice Gray Fan Club
Amazing Mrs. Loquacious
Dr. Teeth
Facedown
Janey Lynne
Maxime Lamy
Ridley Thunder
Stan Woo
Vanilla Con
Wobbly*Bits
I Wish These People Updated More Than Once a Year
Anand Narayan
Jeff Perrin
<< I would have sex with assembla.com if it were a woman (and so should you)
My guest post for caffeinatedcoder.com is now up >>
'Mocking' Request.Form and TempData in the ASP.NET MVC
*
Apologies for the technical interruption - we'll get back to our regular lack of content soon enough - but I was reading over
Ben Scheirman's post on testing TempData
and my comment ended up just long enough that I thought it needed its own post.
One of the current projects I am working on is using the Microsoft MVC. Now, I haven't been screwing with TempData like Ben (haven't had the need) but I *had* previously encountered similar issues with Request.Form. Sure, there's no problem with mocking out IHttpContext and all of its 15 million dependencies therein, but
a) I'm pretty married to the mockRepository.Record(){}/mockRepository.Playback(){} syntax
b) it just seemed like a lot of extra code to get at a NameValueCollection.
Hence, I just busted out
layer supertype
and had a base class with the following pertinent data:
public class MyControllerBase: Controller
{
public virtual NameValueCollection FormData
{
get
{
return Request.Form;
}
}
}
Methods in the inheriting controllers refer to FormData instead of Request.Form in exactly the same way. Let's take a small piece of code from the Igloo of Love application that we'll talk about at a later date...
e.g. Request.Form["lastnameofdonaldsnewlovah"]
becomes FormData["lastnameofdonaldsnewlovah"]
(Note: yes, this is really just an example of utilizing a base class with a testing hook but I prefer dropping "layer supertype" as using larger words == instant credibility).
Now, the reason I do this is because when I need to set up some form values for testing, I want it to be even simpler than with the mocking of IHttpContext, ya da ya da...
thus, in test controller classes (yes, this is testing by inheritance and many of you would rather scrape your nether regions with a board full of nails, but bear with me please) we do the following:
private NameValueCollection privateFormData;
public override NameValueCollection FormData
{
get
{
return privateFormData;
}
}
Now, this makes some of my testing RIDICULOUSLY EASY, because all I need to do in my test is write:
myTestController.FormData["lastnameofdonaldsnewlovah"] = "Bellware";
and we're set to test actions that rely on FormData.
When I first read Ben's post, my initial thought was "Well, why don't we just extend my layer supertype (good job, Justice, remember that term when negotiating salary in the future) to also 'mock' TempData?"
My second thought was
"public TempDataDictionary(IHttpContext httpContext)
{
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
this.HttpContext = httpContext;
this.EnsureReadData();
}"
Yes, for those of you asking, I think in Reflector disassembled code, which I am 7331*.
Third thought was, "Well, this certainly sucks, I'm going to kill myself now."
After the depression phase of my manic depression subsided, I'm back on the "manic" phase to tell you that
no testing hook for TempData == SUPER CRAZY, and since
it seems
I have already successfully taken on 2/3rds of the Microsoft MVC team
, I would like to extend a challenge to
Phil Haack
: for the love of all that is good,
please fix this so I don't need to jump through five flaming hoops and castrate a lion in order to mock out TempData
!
Since
I mean, take a look at all the efforts poor Ben has to go through in order to get this going with mocks! Yike!
I've got your back Ben!! That's what blood brothers do!!
* Yes, this image shows up when searching Google for TempData, confirming the theory I espoused at my presentation on DevTeach 2007 that using the MS MVC is the only guarantee a .NET developer has for
getting LAID
**.
** Unless of course you are as good looking as yours truly*** in which case you should be getting some in between every unit test you write
*** Unfortunately, that's impossible
Thursday, January 31, 2008
Comments [3]
Technical
|
2/1/2008 12:14:07 PM (Mountain Standard Time, UTC-07:00)
I hear you. I'm also writing unit tests and have bumped into this problem. We should hopefully have a fix for the next drop, whenever that may be.
It turns out to be a non-trivial issue because the backing store for TempData is the Session. For the time being, I've seen people use reflection *gag* to get around this.
I just keep hiding behind my "It's a CTP!" shield as we work furiously to fix it. ;)
Haacked
|
haackedAT NOSPAMgmail dot com
5/17/2009 4:15:40 AM (Mountain Daylight Time, UTC-06:00)
How are you! Thanks for the invitation so much. I will try to come back as soon as possible. ;).
I am from Bahamas and now teach English, tell me right I wrote the following sentence: "Seo services, news articles and resources.We offer guaranteed search engine optimizaton results and proven search engine."
With best wishes :D, Thurman.
Chernaya Magiya Seo
|
hanma0AT NOSPAMHotmail dot com
5/29/2009 5:12:52 PM (Mountain Daylight Time, UTC-06:00)
Came here from a google search, half the page is lit up in yellow (the word "the" sure appears alot!) and makes the whole thing hard to read. You should offer a way to turn off this highlighting! Even the sidebars and captcha are highlighted.... IS THIS THGIN WORKING?
Random Guest
Name
E-mail
Home page
Remember Me
Comment (HTML not allowed)
Enter the code shown (prevents robots):
Justice Gray
: a seething cauldron of rampaging masculinity. A fighter! A brother! Your
secret lover
! A strategy consultant with roots in software development, he is no stranger to rescuing projects and people from managerial, technical, and fashion disasters! He also greatly enjoys speaking about himself in the 3rd person.
Gray's Matter Archives
Recent Posts
Just when you thought the development industry couldn't be taken *less* seriously
WebForms: The Whining Continues
Announcement: I am now available for fitness consultations
If you are at PDC, and the future of Microsoft means *anything* to you
JetBrains tries a brand new marketing approach
Tobin Titus is going DOWN: the beginnings of an industry-shaking announcement
Mr T, She-Ra, and the sordid past of Justice A. Gray
Justice Gray's NHibernate War Stories: Don't Use Int If You Mean Enum
Post Categories
BookReviews
Conferences
DasBlog
EDMUG
Goals
HanselmanQuestions
HotDeveloperCorner
ILiveToInform
Leadership
Mac
OtherGrayAreas
Personal
Productivity
Ruby
Suresh
Technical
Transformers
Vancouver
Wii
<
July 2009
>
Sun
Mon
Tue
Wed
Thu
Fri
Sat
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
Sign In