Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

How To Throw Custom Exception
Steve44
#1 Posted : Monday, July 11, 2016 8:55:38 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/11/2016(UTC)
Posts: 2
Location: United States of America

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

Code:

    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);
        }
    }
Remco
#2 Posted : Tuesday, July 12, 2016 1:28:15 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1287 time(s) in 1194 post(s)
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.
Steve44
#3 Posted : Tuesday, July 12, 2016 1:32:47 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 7/11/2016(UTC)
Posts: 2
Location: United States of America

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
Ralf Koban
#4 Posted : Wednesday, July 13, 2016 6:50:14 AM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 5/19/2014(UTC)
Posts: 44
Location: Germany

Thanks: 4 times
Was thanked: 10 time(s) in 9 post(s)
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
1 user thanked Ralf Koban for this useful post.
Remco on 7/13/2016(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.035 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download