开发者

Unit Testing TryUpdateModel in MVC 2 RC awkward behavior

Before using MVC 2 RC I was using MVC 1, the code I used to update my object in my controller was this:

TryUpdateModel(entity, null, null, new[] { "Name", "Controller" });

I was able to Unit Test this controller action just using this in my test:

controller.ValueProvider = myFormCollection.ToValueProvider();

After migrating my code to MVC 2 this no longer works. It keeps failing in the GetValue method implementation in FormCollection, because it verifies that the value you're asking it for is not null o开发者_JS百科r empty. The thing is this code works in runtime, but not in my unit test. I'm not using a prefix so I'm sending NULL as a prefix value.

I've looked in the MVC 2 RC source code and the first value the ValueProvider of the controller looks for is the prefix, so it fails horribly. What provider do I have to assign to controller.ValueProvider so that it can work in my unit test?

Edit: Asking it very briefly, how do I tell TryUpdateModel to NOT use a prefix in my unit test so that it doesn't throw an exception?

Thanks!


Known bug in MVC 2 RC, will be fixed for the next drop.

From a separate email on this issue:

By the way, while unit testing you might want to use the SimpleValueProvider type. (It can be found in the MVC unit test project, so you can copy it to your own project if you want.) It's literally just a Dictionary that also implements IValueProvider. This would be more useful if you want to store values like { "foo", 42 } rather than { "foo", "42" } for unit testing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜