MvcMockHelpers

NOTE AND DISCLAIMER: This is just a little chunks of helper methods, and I happened to use Rhino Mocks, an Open Source Mocking Framework, the talk at Mix. At my last company I introduced TypeMock and we bought it and lately I've been digging on Moq also. I'm not qualified yet to have a dogmatic opinion about which one is better, because they all do similar things. Use the one that makes you happy. I hope to see folks (that's YOU Dear Reader) repost and rewrite these helpers (and better, more complete ones) using all the different mocking tools. Don't consider this to be any kind of "stamp of approval" for one mocking framework over another. Cool?

Anyway, here's the mocking stuff I used in the demo. This is similar to thestuff PhilHa did last year but slightly more complete. Still, this is just the beginning. We'll be hopefully releasing ASP.NET MVC bits on CodePlex maybe monthly. The goal is to release early and often. Eilon and the team have a lot more planned around testing, so remember, this is Preview 2 not Preview 18.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

is missing all the <T> on all mocks. :(. Do you use VS Paste (http://gallery.live.com/liveItemDetail.aspx?li=d8835a5e-28da-4242-82eb-e1a006b083b9&l=8) from WLW? I've never again had a problem with pasting code since I started using it...

Just watched your MIX presentation. I have to say that it was one of the most entertaining I've seen in a long time.

Tony

Sunday, 09 March 2008 11:08:41 UTC

Hey Scott. I've watched your presentation and I just wanted to tell you it was great. Very easy-going, even for a not-native English speaker like me. Real edutainment, which is not always easy in a >1hour talk.

It was great to see you referencing Linux, alt.net and some of the open source tools out there, the push on TDD and CI... and the jokes on ScottGu, too. xD

It was a great presentation. There's one thing I'm still unclear on, though. Is MVC replacing Web Forms?

NO, absolutely not. It's just ANOTHER OPTION to build ASP.NET apps, another web project for you to choose from. WebForms will continue to be fully supported. I think it was clear from the presentation.

btw I like your humour Scott, way to do it. Everyone could see you're foreigner, not from the Microsoft ;), oh wait, already 6 months there ;P

cowgaR

Sunday, 09 March 2008 15:20:25 UTC

Scott,The MVC session at Mix was fantastic. You crammed in the MVC pattern, the new MVC framework, TDD, mocking, and an entertaining delivery that was unmatched in any other session! Keep doing what you are doing! I also wanted to thank you for your graciousness in the scavenger hunt on Tuesday evening. You can read my run-down and see the photo we took on my blog post - Mix08 - Mixing it up with a Scavenger HuntThanks again!

Hi Scott, I just watched your MIX08 session.I have developed my research site by MVC framework since the first preview released. I got the problem when I use site master page. It was discussed by other guys in ASP.NET forum. URL: http://forums.asp.net/t/1195178.aspxIt seems the issue still not be resolved at this new preview 2. I can only use the hacked solution in this thread. Could you take a look the issue?

I took time to watch the presentation this morning. I found it very interesting.

You guys seem to be taking it all very seriously, and I think you are doing a great job so far. Some of the points I have personally raised were addressed in the presentation - thanks.

I think the greatest risk to getting developers to take this on will be what happens once you release version 1.0. Because most of us are inherently lazy, uptake will be slow in enterprise development - it is a lot of new technology to learn. Most MS enterprise developers I know don't even know HTML, which is why they use WebForms in the first place. (It's easy to learn if they know WinForms.) And don't even ask about CSS. NHaml is great too, but it will go over most people's heads.

It seems like your target market are the web developers of the world - just like the audience for Ruby on Rails. Unfortunately it is going to be a long time before IIS7 (necessary for Routing to work properly, I assume) and SQL Server are as mature and cheap to run as Rails and MySQL. (And before you say "SQL Express, dude!", my hoster - webhost4life - just turned SQL Express off because of its poor performance.) If all that means that uptake of ASP.Net MVC is slow, I hope it means you don't give up.

I think your decision to make Routes available to all web apps will be great for me. I will build it into my own apps. Can you use the Html.Action link thing from within WebForms too?

But overall, I really like it. I'm looking forward to seeing the result - and then version 2. :-)

Last year I was excited about the ASP.Net Futures, and Silverlight, because of the DLR. MIX07 was full of dynamic language stuff, and as far as I remember, IronRuby was one of your pet topics. Since then it's gone AWOL, and there is absolutely nothing appearing about it. I personally liked IronPython, but there's no support for it now - i.e. no tutorials, no help, no ASP.Net Futures, etc, etc.

I attended this session at Mix, and it was as entertaining as it was informatitve.

Thanks,Rick

Rick Arthur

Monday, 10 March 2008 01:29:56 UTC

Hi Scott,

The session was very nice and kept me glued all throughout the session. I think having a humourous tone in the talk was really nice. Might not be possible, but such tone should be used in screencasts too! that way learning is more fun!

A very interesting concept, in my opinion, is the combination of ASP.NET MVC and Silverlight 2.0. Can these two be hooked up together? Is it something you guys are thinking of, and is this something that you recommend?

If so, it'd be great to maybe see a screencast on the subject, or a blog post that explains the basic concepts of working with MVC and Silverlight.

Thanks!

Orr Sella

Monday, 10 March 2008 12:41:14 UTC

Hi Scott

Will we ever see ASP.NET MVC on .NET 2, or should I rather go with Spring.NET if I'm looking for an MVC framework for .NET 2.

ThanksHorea

Horea Hopartean

Monday, 10 March 2008 19:40:10 UTC

Hi Scott

I can't work something out. Using your classes + Rhino how do I tell Rhino to expect a redirect? When I execute

Controller.Response.Redirect("/Home/Index");

during record mode I get an error that the method is not implemented!

Thanks

Pete

Peter Morris

Monday, 10 March 2008 20:42:32 UTC

Peter,

You might want to use the "RedirectToAction()" method instead.

In Scott's session he asked us to blog and post about our experiences, so I have begun a post at my blog to record my Wish List. So far the "Wish List" is a single short item: I would like to be able to use the UrlHelper Action method in a Static context. In my example, I tried to use it in my MasterPage definition but could not because I did not have a ViewPage instance.

I'll add to my post above as I come across more items, otherwise, I'm just loving it! I'm sure I'll spend the rest of the month happily ensconced in MVC.

Just some small notes about style for the querystring parsing. First off, you can use the System.Uri class to help a bit (it'll give you in a more strongly-typed way whether there are querystring parameters), like so:

finally, you can add all this as an extension method on the Uri class itself, as follows: public static class UriExtensions { public static NameValueCollection GetQueryStringCollection(this Uri uri) { NameValueCollection parameters = new NameValueCollection();

I think we need an event or partial method that will execute inside the controller prior to the requested Action beginning. It would be very handy to execute Controller wide behavior at this level without needing every Action method to explicitly call another method. I imagine this could be used for logging, validation, security, and many other things.

Add an HtmlHelper method for a simple TableRow: this would be sure to assign the property names automatically to facilitate the use of UpdateFrom().

So far, I really like what I see. I'm trying to work my way through the rest of the videos and examples I can find. I have come across something, and I don't know if it is related to the project or not. I tried to use the Url.ActionLink() method inside a form action attribute and IntelliSense would not work. If I move the code out side the form to an independent code block it works just fine (confirming my references are OK). I tried it both with and without double quotes and no joy either way. It just seems strange to me.

I've been going through the Advanced video, trying to figure out how to implement a site-wide error handler. This should be better than relying on the webserver or developing a custom 404 page. I've watched the section on InterceptController several times, which I think along with a *catchall Route could help me come up with a solution, but I have one burning question: how are HomeController and InterceptController related? I can see where InterceptController is handling whatever Controller is passed in, but I never can see how HomeController is made aware of InterceptController.

Add an HtmlHelper method for a simple TableRow: this would be sure to assign the property names automatically to facilitate the use of UpdateFrom().

Personally, I disagree with this approach, Joel. One of the big upsides to MVC, in my eyes, is that discrete control over the HTML is fairly broadly maintained without having to hack WYSIWYG code. It's a slippery-slope to create tags that are partial to the rest of the structure (i.e., must be within a table), because the next step is to just design the table... and, well, that sort of thinking is what makes such code bloat in the WebForms model.

Very good stuff Scott, add me to your list of fans. I was having a problem mocking my context because I was saving to the database using Linq to sql and part of the save procedure, I check if the user is authenticated and also get the user's name. I add a couple more extension methods to create a Fake authenticated context, it basically mocks the User and identity objects in the context and returns true for IsAuthenticated... You can find the code on my blog right here

I also have to say I like Moq best out of the other testing frameworks.

I tried your sample code with the new MVC refresh but the akeViewEngine.ViewContext was null, I guess the FakeViewEngine class needs to be rewritten to match the new RenderView method.... will try to do that... but woulden't mind having you show it to us as I am all new to mvc and tdd...

how can i add htmlAttributes like form id in Html.Form("CandidateAdd", "Candidate", FormMethod.Post, htmlAttributes) in mvc preview 2 previously it was like this Html.Form("CandidateAdd", "Candidate", new{id="frmcandidate"})