MVC 3 Controller Unit Testing - TryUpdateModel and a few kinds of exception
I ran into serious problem while testing my Controller in ASP.NET MVC 3.0.
Here is one of the controller's methods:
public ActionResult Create(FormCollection collection)
{
IModel model = null;
model = modeldao.createNewObject(ref model);
TryUpdateModel(model, null, null, new[] { "id" });
... // rest
}
The most important par开发者_如何学编程t of the test for method mentioned before is written just like that
Controller controller = new Controller(// list of mocks)
FormCollection form = new FormCollection();
form.Add("name", "object1");
form.Add("parentobject.id", "1");
controller.ValueProvider = form.ToValueProvider();
controller.Create(form);
For now, it throws ArgumentNullException
, even if I mock ControllerContext
object. When I try to set ValueProvider
property in controller:
controller.ValueProvider = form.ToValueProvider();
then I get MethodAccessException
and I'm told to add new assembly: System.ComponentModel.DataAnnotations
.
I have already read a few articles about testing MVC 3 Controller (there's not plenty of them), but their tips simply do not work, though code seems to be similar:
http://www.codecapers.com/post/ASPNET-MVC-Unit-Testing-UpdateModel-and-TryUpdateModel.aspx http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx
I would like to ask if someone has ever encountered this kind of problem and solved it.
精彩评论