Saturday, February 27, 2010

ASP.NET MVC2 RC/VS2010RC – Adventures of the plain old beginner. Part2 – MVC Testing

Continued from Part 1 where I was trying to structure up the solution.

Tools.

After reading “ASP.NET MVC in Action” I’m really keen to try Gallio with MbUnit. As for a mocking framework i decided to ditch Rhino.Mocks this time and go with Moq. I’ve heard so many praises from my fellas on it, so it can time to learn new tricks. I gave a last chance to Rhino.Mocks by going to the download page to see the latest changes and found a last release being 4 months old there. Good-bye Rhino.Mocks!

Gallio and MbUnit come in a bundle. You can get it here http://code.google.com/p/mb-unit/downloads/list.

When picking a download on Moq project I started to feel a bit of anxiety when seeing Moq.Silverlight binaries to download. Will those be some manual mocks for Silverlight? Can’t wait to explore in the next installment. You can get your copy here http://code.google.com/p/moq/. image

 

A look at the default test generated by VS.

When executing the test, I first thought VS is frozen, but then I understood that actually VS was starting a browser, first test took around 10s and probably it will be faster then. But wait, is this a unit test then?!

/// <summary> 
///A test for Index 
///</summary> 
// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example, 
// http://.../Default.aspx). This is necessary for the unit test to be executed on the web server, 
// whether you are testing a page, web service, or a WCF service. 
[TestMethod()] 
[HostType("ASP.NET")] 
[AspNetDevelopmentServerHost("C:\\dev\\Advaton\\src\\Advaton.EventViewer.WebHost", "/")] 
[UrlToTest("http://localhost:1090/")] 
public void IndexTest() 
{ 
    HomeController target = new HomeController(); // TODO: Initialize to an appropriate value 
    ActionResult actual; 
    actual = target.Index(); 
    Assert.AreEqual(expected, actual); 
    Assert.Inconclusive("Verify the correctness of this test method."); 
} 

My intent would be to have real unit tests and only after that creating integration/ui tests with Gallio/MbUnit and WatiN.

Employing Gallio/MbUNit for MVC unit tests.

image First big and nice surprise to me was ability to run unit tests via Gallio directly from the VS Run Tests button.
I tried MbUnit and Nunit tests and both worked in the same way via Gallio integrated runner.
Only in case of NUnit I had to downgrade to version of Nunit bundled with Gallio as it was reporting a version conflict.
Still it was very nice to have helpful and descriptive messages from the runner in the output window.

This is it for running unit tests. I heard there are some quirks still with Gallio/VS2010RC integration and for my self I witnessed MbUNitv3 template unit test project not being created when creating an MVC project. But still, this is COOL!!!

Creating own unit testing project template.

As MbUnitv3 template didn’t work I wanted to have then my own test template. I followed the approach shown in “ASP.NET MVC in Action” but ran into registry entries mentioned in the book didn’t even nearly exist. Most probably the registration approach has changed in VS2010RC.

Still good enough for me just to have a template from what I found to be a minimum working solution for unit tests:

image

VS is nicely offering the option to import the template after the export.

Getting some lame UI test in place.

This time going to use guidance from the “ASP.NET MVC in Action”. You can get WatiN from http://watin.sourceforge.net/.

image Right at the start, issue with shdocvw.dll. Here is the link to the solution http://geekswithblogs.net/coredump/archive/2007/09/06/115181.aspx – reference ShDocVw.dll from the System32 directory.

ShDocVw fixed now running into:

System.Runtime.InteropServices.COMException: The interface is unknown. (Exception from HRESULT: 0x800706B5)
at SHDocVw.InternetExplorerClass.set_Visible(Boolean pBool)

I worked around this one by running VS as an administrator (SHAME!) http://codeblog.theg2.net/2009/12/watin-20-and-gallio-31-in-x64.html with additional info on stackoverflow http://stackoverflow.com/questions/1014815/how-do-you-get-watin-to-work-on-windows-server-2008-with-ie8

Anyway, got it working! One more thing was to set it up on IIS on my Windows 7 so it had more taste of an “integration” testing. image

This is it for the sprint planned.

Next.

Integrating Silverlight into the solution and seeing what are options for its testing.

Platform and tools: Windows 7, VS2010RC, ASP.NET MVC2 RC, Gallio, MbUnit, NUnit, Moq, Silverlight 3.0

No comments: