Arg.Any<Nullable<T>> and Arg.Any<T?> throws a null argument exception

3 posts, 1 answers
  1. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 30 Jun 2010 Link to this post

    I'm trying to mock out a call that takes in nullable GUIDs, unfortunately when I try arranging the call:

    Mock.Arrange(() => myStubInstance.DoSomthing(Arg.Any<Nullable<GUID>>()) or
    Mock.Arrange(() => myStubInstance.DoSomthing(Arg.Any<GUID?>())

    I get a null reference exception being thrown.  I then tried change the Arrange to use just GUID as the Arg type which works, but when the call is made in the code under test with a null value, the null reference is thrown again.

    How can I get around this?

    Ta,
    J
  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 02 Jul 2010 Link to this post

    Hi John,

    Unfortunately, the issue is more internal of how the argument is matched for nullable types. But i fixed it and it will be shipping with next build.

    With the new build you will be able to do it like [as expected]:
    [TestMethod]
    public void ShouldMatchNullableArgument()
    {
        var foo = Mock.Create<Foo>();
        Mock.Arrange(() => foo.Echo(Arg.IsAny<int?>())).Returns(10);
        int ret = foo.Echo(4);
        Assert.Equal(10, ret);
    }
     
    Thanks a lot for reporting it :-)

    Regards,
    Mehfuz.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. Imaji
    Imaji avatar
    15 posts
    Member since:
    Apr 2007

    Posted 02 Jul 2010 Link to this post

    Rocking, thanks Mehfuz :)
Back to Top