chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I'm hitting a null reference exception in my arrange calls.

Am I doing something wrong or have I found a bug?

I think the issue may be with WithExactArguments()?

Here's a sample project that demos the issue:

    public interface ISearch2
    {
        string GetString(int type);

        string GetFromGuid(Guid g);
    }

    public interface IProvision
    {
        ISearch2 Search { get; }
    }

    public abstract class BaseClass2
    {
        protected string GetFromGuid(IProvision p, Guid g)
        {
            return p.Search.GetFromGuid(g);
        }
    }

    [TestClass]
    public class UnitTest2
    {
        [TestMethod]
        public void UnderTest_Scenerio_Behaviour()
        {
            var mockInterface = Isolate.Fake.Instance<IProvision>();

            Guid myGuid = new Guid("{817B3757-F836-478B-B327-77501F8182EA}");
            Guid myGuid2 = new Guid("{8B4964BB-B6D8-4930-B38F-C32482E1FFBD}");

            Isolate.WhenCalled(() => mockInterface.Search.GetFromGuid(myGuid)).WithExactArguments().WillReturn("one");
            Isolate.WhenCalled(() => mockInterface.Search.GetFromGuid(myGuid2)).WithExactArguments().WillReturn("two"); //XXX this throws a null reference!!

            var mockBase = Isolate.Fake.Instance<BaseClass2>();
            Isolate.NonPublic.WhenCalled(mockBase, "GetFromGuid").CallOriginal();

            string s = Isolate.Invoke.Method(mockBase, "GetFromGuid", mockInterface, myGuid) as string;

            Isolate.Verify.WasCalledWithExactArguments(() => mockInterface.Search.GetFromGuid(myGuid));

            Assert.IsNotNull(s);
            Assert.AreEqual(s, "one");
        }
asked by solidstore (3.2k points)

2 Answers

0 votes
Hey,

Unfortunately it looks like a bug :evil:

Let me investigate some more, and I will update on this thread when I have more information...
answered by igal (5.7k points)
0 votes
Hi again,

It seems that we have a bug with chained calls when used with exact arguments.

As a workaround, you could extract "mockInterface.Search" into a local variable, thus eliminating the chained call. This should make your test pass:

ISearch2 search2 = mockInterface.Search;

Isolate.WhenCalled(() => search2.GetFromGuid(myGuid)).WithExactArguments().WillReturn("one"); 
Isolate.WhenCalled(() => search2.GetFromGuid(myGuid2)).WithExactArguments().WillReturn("two"); 
...
Isolate.Verify.WasCalledWithExactArguments(() => search2.GetFromGuid(myGuid));


Hope that helps
answered by igal (5.7k points)
...