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
Hi

How do I mock overloaded methods when parameters are required?
If I use the following code a NullReferenceException is thrown on the second lambda expression.

using System;
using NUnit.Framework;
using TypeMock.ArrangeActAssert;

namespace Test
{
    public interface ITestInterface
    {
        string LoadText(string text, string prefix, string postfix);
        string LoadText(string text, string prefix);
    }
    
    [TestFixture]
    public class InterfaceTest
    {
        
        [Isolated, Test]
        public void MyTest()
        {
            var mock = Isolate.Fake.Instance<ITestInterface>();
            //works
            Isolate.WhenCalled(() => mock.LoadText(null, null))
                .DoInstead(context =>
                {
                    string paramText = context.Parameters[0] as string;
                    string paramPrefix = context.Parameters[1] as string;
                    return paramPrefix + paramText;
                });
            //thows NullReferenceException
            Isolate.WhenCalled(() => mock.LoadText(null, null, null))
                .DoInstead(context =>
                {
                    string paramText = context.Parameters[0] as string;
                    string paramPrefix = context.Parameters[1] as string;
                    string paramPostfix = context.Parameters[2] as string;
                    return paramPrefix + paramText + paramPostfix;
                });

            Assert.AreEqual("FooBar", mock.LoadText("Bar", "Foo"));
            Assert.AreEqual("FooBar2", mock.LoadText("Bar", "Foo", "2"));
        }
    }
}
asked by gorchard (600 points)

5 Answers

0 votes
Hi,

It seems like a bug :( I'll try to reproduce it and update on this thread.

As a workaround, you can use single WhenCalled (doesn't matter on which overload) and use a condition on the number of parameters to choose the callback behavior.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
We have also encountered this problem in 6.0.8; with the sample snippet provided below. We wanted to execute a different set of statements for different overloads of the same Insert method.

Cache fakeCache = new Cache();
var cacheDictionary = new Dictionary<string, object>();
Isolate.WhenCalled(() => fakeCache.Insert(null, null))
   .DoInstead(callContext =>
      {
         cacheDictionary[callContext.Parameters[0] as string] = callContext.Parameters[1];
      });
Isolate.WhenCalled(() => fakeCache.Insert(null, null, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, null))
   .DoInstead(callContext =>
      {
         var x = callContext.Parameters[5] as CacheItemUpdateCallback;
         cacheDictionary[callContext.Parameters[0] as string] = callContext.Parameters[1];
      });



The workaround that was mentioned worked for us. However, it does rely on checking the parameter count, and possibly the parameter types, to selectively execute statements.

Hopefully, it will be addressed in a future release.
answered by pwong (180 points)
0 votes
Hi,

We are working on a fix, and will post here when we are done.

Regards,
Yonatan
Typemock Support
answered by yonatan (1.6k points)
0 votes
Hi,

A fix can be found in the next releases, and in this patch:
https://www.typemock.com/patches/Typemoc ... .9.527.msi

Regards,
Yonatan
Typemock Support
answered by yonatan (1.6k points)
0 votes
here ,some bugs are generates in this program
answered by sagarmendapara (180 points)
...