Build/Test Issues

How To Throw Custom Exception

Started by Steve44 on 4,449 views

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).


    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);
        }
    }

Edited

Hi Steve,

Thanks for posting.

If I understand what is happening here, this doesn't seem to be a problem related to NCrunch itself.

I believe this code is throwing an NRE on the _queryDispatcher.Dispatch method because in the context of the test, the _queryDispatcher field is never set.

I assume that the intention here was to implement a constructor on the ClientDetailController allowing the _queryDispatcher to be injected. For any kind of mock, stub, or substitute to be effective, it must be provided to the code under test in some form.
I agree the problem is not related to NCrunch itself.

I think we are using NInject to set the dependencies, and NSubstitute to mock them, and I've never been trained in any of this. I hoped someone understood what is happening. In other tests we are doing identity setup with the substitute dispatcher, but in this one it returns null reference error, so I don't think your suggestion is the solution or maybe even practical in our environment. I'll try asking the other developers here.

Thanks
Hi,

excuse me for jumping in.

The issue is not related to NCrunch.
It is (most probably) how you use/define the mocks.

Your test defines a GetClientDetailQuery instance while your code creates another GetClientDetailQuery instance. So there are 2 different living instances hanging around.
As long as you did not override / implement the equality methods (object.Equals, IEquatable<T>.Equals), the mock framework (NSubstitute, Moq....) considers both to be different.
As they are considered different, you did not specify a return value for the specific method call, so per default most Mock frameworks will return null.
As it returns null, you get a NullPointerException.

_queryDispatcher.Dispatch<GetClientDetailQuery, GetClientDetailRequestQueryResults>(query) <-- returns null

If you want to make it pass, either implement the equality members or use the helper classes from the Mock framework, such as "Arg.Is<>(...)" or how it's named in NSubstitute.


Best regards,
Ralf

Post a reply

Log in to reply.