views:

160

answers:

3

When I migrated sample SportsStore app from Steve Sanderson's Pro ASP.NET MVC Framework (from asp.net 1.0 to mvc 2 beta) using this app provided by eric lipton, everything work just fine - except 2 unit tests.
The error message on both is:
Tests.CartControllerTests.VeryLongTestMethodName:
System.ArgumentNullException: value can't be undefined.
Parameter name: context

I suspect this is because default model binder in version 2 supports DataAnnotations, because stack call trace from NUnit told me about some problems in DefaultModelBinder. Any ideas how can I fix it?
EDIT
Tried to use Moq to solve the problem, but it didn't work. Here's the code:

var request = new Moq.Mock<HttpRequestBase>();
request.Setup(r => r.HttpMethod).Returns("POST");
var mockHttpContext = new Moq.Mock<HttpContextBase>();
mockHttpContext.Setup(c => c.Request).Returns(request.Object);
controllerContext = new ControllerContext(mockHttpContext.Object, new RouteData(),
                            new Moq.Mock<ControllerBase>().Object);

Method I'm testing submits only POST. Is it ok to put "POST" to my tests?

+1  A: 

Suspect this may be an omission in the converter. If this is a (sample) application then can't you simply open the application in 2.0 and forget about the conversion?

If you are trying to learn 2.0 then this may not be the best way forward.

If you are testing to see whether the converter works then maybe try it on a real life application and not a sample one.

Have you considered leaving a comment on the converters web site given that any comments left there would be specific to the converter?

Have you actually tried to step through the code line by line and see if there is anything obvious.

Anyway, these are the things I'd be trying.

griegs
+1  A: 

To get this to work in ASP.NET MVC 2 you'll have to create a dummy ControllerContext and set it on your controller in your unit test's intialization (or "Setup" or "Arrange") section.

Here's an example of how to create a dummy ControllerContext with the help of the Moq library: http://stackoverflow.com/questions/1206921/how-to-mock-the-controller-context-with-moq

Eilon
it seems like I've done all things as this examples says, but it still don't work:( posted stack trace in question
chester89
A: 

I believe that this is still a problem in the ASP.NET MVC 2.0 release. There is a problem with the FormCollection object when used with UpdateModel/TryUpdateModel throws an exception. See http://stackoverflow.com/questions/1936970/asp-net-mvc-2-problem-with-updatemodel

I recently tried to upgrade my project, but have decided to stick with MVC 1.0 for now until the RTM (which is meant to have a fix).

Matt Tester
The problem I referred to is with the MVC 2.0 R1 release. The new MVC 2.0 R2 release has fixed this error. Upgrading should solve the problem.
Matt Tester