I'm in the process of writing 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));
}
}
The 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
{
}
}
The exception occurs when the Create
controller action attempts to to call the ToDataSourceResult
method.
System.EntryPointNotFoundException : Entry point was not found.
Debugging shows that the ModelState internal dictionary is empty in the unit test (and not when run in a standard context). If the ModelState
is removed from the ToDataSourceResult
method then the test passes successfully. Any help is much appreciated.