public class ClientDetailController : ApiController
{
private readonly IQueryDispatcher _queryDispatcher;
public IHttpActionResult GetCustomerByClientfolderId(int clientfolderId)
{
var query = new GetClientDetailQuery() { GetClientDetailRequest = new GetClientDetailRequest() { ClientFolderId = clientfolderId } };
try
{
GetClientDetailResponse response = _queryDispatcher.Dispatch<GetClientDetailQuery, GetClientDetailRequestQueryResults>(query).GetClientDetailResponse;
return Ok(response);
}
catch (NotFoundException)
{
return NotFound();
}
}
}
[TestFixture]
public class ClientDetailControllerTests
{
private IQueryDispatcher _fakeDispatcher;
private ClientDetailController _clientDetailController;
[SetUp]
public void init()
{
_fakeDispatcher = Substitute.For<IQueryDispatcher>();
_clientDetailController = new ClientDetailController(_fakeDispatcher);
}
[Test]
public void GivenInvalidClientFolderId_GetCustomerByClientFolderIdReturnsNotFound()
{
var _fakeClientFolderResquest = 0;
var query = new GetClientDetailQuery() { GetClientDetailRequest = new GetClientDetailRequest() { ClientFolderId = _fakeClientFolderResquest } };
_fakeDispatcher.Dispatch<GetClientDetailQuery, GetClientDetailRequestQueryResults>(query).Returns(x => { throw new NotFoundException(); });
var actionResult = _clientDetailController.GetCustomerByClientfolderId(_fakeClientFolderResquest);
Assert.IsInstanceOf<NotFoundResult>(actionResult);
}
}
I'm new to NCrunch, but I have coded a few tests that have worked. Now I'm trying to test a catch clause for a custom exception that I created. Below is the try/catch and below that is the test code. When it calls the Dispatch method it encounters a null reference exception, but I can't determine what is null. Can anyone see some obvious mistake that I am making? We're using NSubstitute to create our mock objects (I think that's what it is, I'm new to that, too).
Edited 11 Jul 2016 21:05 UTC