This is a migrated thread and some comments may be shown as answers.

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

1 Answer 240 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Asim
Top achievements
Rank 1
Asim asked on 03 Aug 2011, 05:59 PM
Hi!

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

 

Mock.NonPublic.Arrange<bool>(

target,

"IsUSPriceChangeOnCanadianAdoptedRSTitle",

mockedDBTrans,

Arg.IsAny<TitleRequestDataset.TitleRequestRow>())

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

.Returns(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.

Thanks,
Asim

1 Answer, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 08 Aug 2011, 02:51 PM
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
    {
        get
        {
            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,
Mehfuz
the Telerik team

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

Tags
General Discussions
Asked by
Asim
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or