Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 3 Controller Unit Testing - TryUpdateModel and a few kinds of exception
    primarykey
    data
    text
    <p>I ran into serious problem while testing my Controller in ASP.NET MVC 3.0.</p> <p>Here is one of the controller's methods:</p> <pre><code> public ActionResult Create(FormCollection collection) { IModel model = null; model = modeldao.createNewObject(ref model); TryUpdateModel(model, null, null, new[] { "id" }); ... // rest } </code></pre> <p>The most important part of the test for method mentioned before is written just like that</p> <pre><code>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); </code></pre> <p>For now, it throws <code>ArgumentNullException</code>, even if I mock <code>ControllerContext</code> object. When I try to set <code>ValueProvider</code> property in controller:</p> <pre><code>controller.ValueProvider = form.ToValueProvider(); </code></pre> <p>then I get <code>MethodAccessException</code> and I'm told to add new assembly: <code>System.ComponentModel.DataAnnotations</code>.</p> <p>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:</p> <p><a href="http://www.codecapers.com/post/ASPNET-MVC-Unit-Testing-UpdateModel-and-TryUpdateModel.aspx" rel="nofollow">http://www.codecapers.com/post/ASPNET-MVC-Unit-Testing-UpdateModel-and-TryUpdateModel.aspx</a> http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx</p> <p>I would like to ask if someone has ever encountered this kind of problem and solved it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload