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 have a NaturalMock setup that I want to check the 1st and 2nd parameters of a params array against LogMessage, shown below.

public void LogMessage(string message, params object[] messageArgs)


I do not care what the value of the 1st parameter (message) is.

The following test fails with VerifyMocks as follows:

Test method TeamHelp.Build.Tasks.Utilities.MSBuildRunnerTest.RunLogsItsStartUp threw exception: TypeMock.TypeMockException:
*** Too Many Arguments to validate (expected up to 2 but found 3 ).

What am I doing wrong?

Thanx

Tim




        [TestMethod]
        public void RunLogsItsStartUp()
        {
            MSBuildRunner target = this.GetTestableTarget();
            MSBuildRunner_Accessor a = MSBuildRunner_Accessor.AttachShadow(target);

            string path = @"c:
othing.exe";
            string arguments = "arguments";

            using (RecordExpectations r = new RecordExpectations())
            {
                target.Log.LogMessage(string.Empty, path, arguments);
                r.CheckArguments( Check.IsAny(), path, arguments );
            }

            a.Run( path, arguments );

            
        }
asked by tbassett (1.8k points)

4 Answers

0 votes
Hi Tim,

Just wanted to let you know I'll look into this and get back to you shortly.

Doron,
Typemock Support
answered by doron (17.2k points)
0 votes
Tim,
When c# encounters a params argument it is really taking all the arguments, packing them up in an array and passing the array.
So calling
target.Log.LogMessage(string.Empty, path, arguments);

is really calling
object[] temp = new object[] {path, arguments};
target.Log.LogMessage(string.Empty, temp);


once we understand this it is easy to fix the code:
using (RecordExpectations r = new RecordExpectations())
{
     target.Log.LogMessage(null); // arguments ignored
     r.CheckArguments( Check.IsAny(), new object[] {path, arguments} );
}
answered by eli (5.7k points)
0 votes
Tim,
When c# encounters a params argument it is really taking all the arguments, packing them up in an array ...


Thanx, I'll give it shot when I have a chance...
answered by tbassett (1.8k points)
0 votes
Tim,

There's another option to accomplish the same thing using the Natural Mocks syntax. Natural Mocks support checking the arguments passed to a method by calling CheckArguments() after the call with no parameters:

using (RecordExpectations rec = RecorderManager.StartRecording())
{
     target.Log.LogMessage(string.Empty, path, arguments); 
     rec.CheckArguments();
}


This way we set an expectation against the same arguments passed in the last recorded call. By providing parameters to CheckArguments() you override this mechanism, and in order to check a variable number of arguments you will need to pack them in an object[], like in Eli's example.

I hope this clears things up. Please let us know if this helped.

Doron,
Typemock Support
answered by doron (17.2k points)
...