Problem using Arg.IsAny<T> when mocking private method

2 posts, 0 answers
  1. Asim
    Asim avatar
    7 posts
    Member since:
    Jul 2011

    Posted 03 Aug 2011 Link to this post


    When I use Arg.IsAny in the arrange for a private method, as follows:







    .DoInstead( () => {calledIsUSPriceChangeOnCanadianAdoptedRSTitle =  true;} )


    I get an ArgumentException at run time with the following message:

    Direct null valued argument for non-public member is not supported. Use ArgExpr.IsNull<T>() or other members from ArgExpr wrapper class to provide your specification.

    Please advise on how to get around this.


  2. Ricky
    Ricky avatar
    467 posts

    Posted 08 Aug 2011 Link to this post

    Hi Asim,

    Thanks again for sending the issue. For mocking non-public members instead of using Arg.IsAny<T> you have to use ArgExpr.IsAny<T>.

    Here I created a sample test using ArgExpr.IsAny<T> that worked as expected:
    public void ShouldInvokeFakeMethodWhenSetupWithArgIsAnyForNonPublic()
        var foo = new Foo();
        Mock.NonPublic.Arrange<string>(foo, "GetConnection", ArgExpr.IsAny<string>()).Returns("telerik");
        Assert.AreEqual("telerik", foo.Connection);

    The class for which the sample test is created looks like:
    public class Foo
        public string Connection
                return GetConnection("telerik");
        private string GetConnection(string connection)
            return string.Empty;

    Hope that answers your question. Please let me know if you still having issues.

    Kind Regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Back to Top