Posted by Sebastien Lachance on May 14, 2008
Listening to the last screencast of Rob Conery (MVC Storefront) made me realize something that I haven’t really understood before.
Suppose you have the interface of a repository, let’s say IEntryRepository. Then you have the actual SqlEntryRepository and TestEntryRepository. When I am doing TDD, I am testing the TestEntryRepository. But it will not be used in production. Why bothering with testing it then?
Because TDD is a design process. Even if I am testing my code, I am also designing it. The SqlEntryRepository will benefits from the actual design of the repository we use for testing.
Posted in ASP.NET MVC, Agile, Design, Learning, Tests | 2 Comments »
Posted by Sebastien Lachance on May 12, 2008
Like promised, on a previous post, I will show you how to mock HttpRequest. I will use Rhino Mock but you can use the one you want (moq, typemock, etc).
On you controller base class, a property called Request is exposed. Behind the scene you are accessing an HttpContextBase instance (this class is contained in the System.Web.Abstractions library), which is providing you the HttpRequest you want to mock.
So, let’s start by creating a mock of these two object.
var mocks = new MockRepository();
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
The mockedHttpRequest will be provided by the mockedHttpContext. So we will setup the mocked HttpContextBase.Request property to return the mocked HttpRequestBase.
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
But, what would we do with a mocked HttpRequestBase? When you need to test an action on a controller that will retrieve values for the Request.Form property you could create a NameValueCollection and tell the Form property of the HttpRequestBase instance to return it.
NameValueCollection formParameters = new NameValueCollection();
formParameters.Add("txtUsername", "username");
formParameters.Add("txtPassword", "password1");
SetupResult.For(mockedHttpRequest.Form).Return(formParameters);
I really like playing with the ASP.NET MVC framework!!!
Posted in .NET, ASP.NET MVC, Learning, Tests | No Comments »
Posted by Sebastien Lachance on May 8, 2008
I just got a hard time figuring how I could be mocking the HttpContext so that I would be able to mock HttpRequest. But once you understand the principle, it is fairly easy.
First thing we need to know is that the HttpContext is held inside a ControllerContext object. Once we have instantiated one, we can then put it inside our controller.
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
Where mockedHttpContext is your mocked HttpContextBase object.
Inside the HttpContextBase object reside a lot of useful stuff that can replace with our mocks. By example, the HttpRequest, HttpServer, HttpApplication, etc, are all contained inside. So, getting familiar with this basic knowledge is a must to do testing usefully.
Next post : Mocking HttpRequest.Form to return what we want.
Posted in .NET, ASP.NET MVC, Learning, Tests | 1 Comment »
Posted by Sebastien Lachance on May 6, 2008
I haven’t really done any testing in ASP.NET MVC until now. I started the development of a more serious application and decided to do it TDD. My first test was to make sure the Index action render the Index view. So as an informed developer, I started reading some blog posts to discover that in order to test a controller you need to mock a lot of things. I followed the screencast of Scott Hanselman and implemented his MvcMockHelper. It didn’t worked. The ViewContext on the view engine remained null, resulting in an NullReferenceException. I downloaded the source of the last build to see for myself what was wrong. Surely I don’t understand, because nowhere in the source they are setting the ViewContext on the ViewEngine. They just fill a RenderViewResult object and return it back.
This is how I figured out how we can now test controllers. With the new Interim build of the 04/16, we don’t have to mock the HttpContext, HttpRequest and so on anymore. No need to do a fake view engine and no more mocking needed to test the RenderView method.
[Test]
public void Index_Should_Set_The_Index_View()
{
EntryController controller = new EntryController();
var result = (RenderViewResult)controller.Index();
Assert.That(result.ViewName, Is.EqualTo("Index"));
}
So if you have any other operations (RedirectToAction and Redirect for example), it will return an object of type ActionResult that you can cast to the appropriate inheritor (RenderViewResult, ActionRedirectResult, HttpRedirectResult) to get a lot of information that you would have to retrieve manually with a mock.
I really like the simplicity of it and can’t wait to see more improvement. This is an immense opportunity to learn a great deal of different things.
Posted in .NET, ASP.NET MVC, Learning | 1 Comment »
Posted by Sebastien Lachance on May 6, 2008
This is intended to be a quick start for anyone who want to start playing with the latest version of ASP.NET MVC without going through the origin and goals of the MVC pattern. I will use the ASP.NET 0416 MVC Interim Source Code Release for this. Keep in mind, that this will probably all change in a near future.
First step, download and build the source. You will need to have the Moq library (more about this mocking framework in a future post).
Second, download and install the Visual Studio MVC Templates (execute the vsi file).
Third, start an ASP.NET MVC project.
I have two useful resources to share with you. I believe they will give you a head start.
You have the Scott Hanselman’s screencast series on the ASP.NET web site. And you have the MVC Storefront series from Rob Conery.
Posted in .NET, ASP.NET MVC, Agile, Learning | No Comments »