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() {
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.
source share