Test Management Module Actions in MVC4 with the Kendo UI

I am going to write some unit tests for our controllers. We have the following simple controller.

public class ClientController : Controller { [HttpPost] public ActionResult Create(Client client, [DataSourceRequest] DataSourceRequest request) { if (ModelState.IsValid) { clientRepo.InsertClient(client); } return Json(new[] {client}.ToDataSourceResult(request, ModelState)); } } 

unit test for this is as follows:

 [Test] public void Create() { // Arrange clientController.ModelState.Clear(); // Act JsonResult json = clientController.Create(this.clientDto, this.dataSourceRequest) as JsonResult; // Assert Assert.IsNotNull(json); } 

And the controller context is faked with the following code:

  public class FakeControllerContext : ControllerContext { HttpContextBase context = new FakeHttpContext(); public override HttpContextBase HttpContext { get { return context; } set { context = value; } } } public class FakeHttpContext : HttpContextBase { public HttpRequestBase request = new FakeHttpRequest(); public HttpResponseBase response = new FakeHttpResponse(); public override HttpRequestBase Request { get { return request; } } public override HttpResponseBase Response { get { return response; } } } public class FakeHttpRequest : HttpRequestBase { } public class FakeHttpResponse : HttpResponseBase { } } 

An exception is ToDataSourceResult when the action of the Create controller tries to call the ToDataSourceResult method.

 System.EntryPointNotFoundException : Entry point was not found. 

Debugging shows that the ModelState internal dictionary is empty in unit test (and not when launched in the standard context). If the ModelState is removed from the ToDataSourceResult method, then the test succeeds. Any help is greatly appreciated.

+6
source share
1 answer

The fast peak of JustDecompile shows that Kendo.Web.Mvc.dll was created against System.Web.Mvc version 3.0. Your test project probably references the new version of ASP.NET MVC (4.0), and therefore at runtime, any calls to System.Web.Mvc members result in a System.EntryPointNotFoundException because these members cannot be resolved. In your particular case, the culprit of the call was a call to the KendoUI MVC extension method ToDataSourceResult() and its subsequent call to ModelState.IsValid .

The reason all this works without errors in your application is because your project is configured by default as part of the ASP.NET MVC Visual Studio project template to redirect assembly bindings so that the runtime targets the latest version of ASP.NET MVC collected. You can fix your test project by adding the same binding information to the App.config file at runtime:

 <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> 

I hope this helps.

+5
source

Source: https://habr.com/ru/post/947642/


All Articles