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
Hello,
I've two static method with the same name but with different number of parameters. One function is invoke from other:

public static DataSet GetDataSet(string storedProcedure, SqlParameter[] arParams)
        {
            string connectionString = (string)Settings.ConnectionString[Datasource.MyDatasource];
            return GetDataSet(storedProcedure, arParams, connectionString);
        }

        public static DataSet GetDataSet(string storedProcedure, SqlParameter[] arParams, string connectionString)
        {
            return GetDataSet(false, storedProcedure, arParams, connectionString);
        }


I want to test first method so i should check if second method is invoke with correct parameters. I do it as below:

        [Test]
        public void GetDataSetTest1()
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            using (RecordExpectations r = RecorderManager.StartRecording())
            {
                r.ExpectAndReturn(Settings.ConnectionString[""], ht);
                r.ExpectAndReturn(Common.GetDataSet("storedProcedure", null, null), new System.Data.DataSet()).CheckArguments();

            }
            Common.GetDataSet("storedProcedure", null);
        }


Unfortunately i get error:
GetDataSetTest1 : TypeMock.VerifyException :
TypeMock Verification: Call to Common.GetDataSet() expected 3 arguments but was called with 2

When I change name of one of my static methods it works OK, but I cant change it. Many references......

Is another solution for this case?
br
Tadeusz
asked by tlucz (2.9k points)

4 Answers

0 votes
hi,
supporting overload method is one of the limitation of the isolator framework. therefore we you cant directly do that.

what you can do is setting an expectation of the first call that will activate the original method and then setting a second expectation (this time a real one) for the second call.


let me know if you need more specifics on how to do that.
answered by lior (13.2k points)
0 votes
hi,
I tried do it this way:

using (RecordExpectations r = RecorderManager.StartRecording())
         {
             r.ExpectAndCallOriginal(Common.GetDataSet("storedProcedure", null));
             r.ExpectAndReturn( Common.GetDataSet("storedProcedure", null, null),new System.Data.DataSet());
         }
         Common.GetDataSet("storedProcedure", null);


but get an error:

CommonTest.GetDataSetTest1 : System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> System.ArgumentException : An item with the same key has already been added.


Thanks for any tips for this problem.
br
Tadeusz
answered by tlucz (2.9k points)
0 votes
Hi Tadeusz,

I've cooked up a more complete example for your case.
The example is based on the code snippets you have published so I hope it will do its work for you.
if possible please let me know if this works for you.

[TestMethod]
[VerifyMocks]
public void OverLoadWorkArround()
{
    DataSet fakeDataSet = new DataSet();
    Hashtable fakeHash = new Hashtable();
    fakeHash[""] = "dummy";

    using (RecordExpectations recordExpectations = new RecordExpectations())
    {
        //some expectation in order to get the connection string calls to pass
        string dum = Datasource.MyDatasource;
        recordExpectations.Return("");

        Hashtable dum2 = Settings.ConnectionString;
        recordExpectations.Return(fakeHash);
        //the first call (the one with the two arguments) is passed to original code.
        Common.GetDataSet("", null);
        recordExpectations.CallOriginal();
        //the second call (three arguments) - is mocked.
        Common.GetDataSet("storedProcedure", null, "dummy");
        recordExpectations.Return(fakeDataSet).CheckArguments();
    }

    Common.GetDataSet("storedProcedure", null);
}
answered by lior (13.2k points)
0 votes
Thanks, it's working. My test code works too. Exception was caused by other test method without VefifyMocks. My faults.

Once again, thanks for answer.
br
Tadeusz
answered by tlucz (2.9k points)
...