0

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?

3
  • Can you show the full stack trace from the failing unit test? Commented Dec 1, 2009 at 20:48
  • And my name is Eilon Lipton - not Eric Lipton :) Commented Dec 1, 2009 at 20:48
  • I'm sorry for the name yep, can provide the stack Commented Dec 1, 2009 at 20:57

3 Answers 3

2

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: How to mock the controller context with moq

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

Comments

0

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 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).

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.