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 discovered a bug, while trying to mock a function with a out-parameter. All works fine as long as no parameter-verification is done, but once RecordExpectations.CheckArguments([..]) is used, the out-parameter will only return null.

Steps to reproduce:

1. Take the example found in your documentation (https://www.typemock.com/Docs/UserGuide/ ... ndOut.html) and slightly modify it, so that it actually compiles (and shows the bug). The result will look as follows:

public class ToBeTested
{
   // C#
   // An example method with ref and out parameters
   public static int SomeMethod(ref string name, out int[] list)
   {
      // Do Something
      list = new int[1];
      return 0;
   }
}


[Test]
public void RefReturnValues()
{ 
   string returnNameValue = "TypeMock"; // we will return for ref parameter
   int[] returnListValue = new int[5]; // we will return for out parameter
  
   using (RecordExpectations recorder = new RecordExpectations())
   {
      // CAUTION: ALL calls here are mocked!!!
      ToBeTested.SomeMethod(ref returnNameValue, out returnListValue);
      recorder.CheckArguments(Check.IsAny(), Check.IsAny());
      recorder.Return(3);
   }
   // mock returns 3, sets name to "TypeMock" and list to a 5 element array
   // so this test will pass
   string name = "";
   int[] list;
   Assert.AreEqual(3,ToBeTested.SomeMethod(ref name, out list));
   Assert.AreEqual("TypeMock",name);
   Assert.AreEqual(5,list.Length);
   MockManager.Verify();
}


2. Run the test


Expected results:
-> The test should perform fine

Actual results:
-> The test fails, as name is still "" and list is still null!

Workaround (not really):
-> Remove the following line: recorder.CheckArguments(Check.IsAny(), Check.IsAny());
asked by philipp (2.9k points)

4 Answers

0 votes
This is a case where TypeMock.NET default behavior confuse the poor user. :(
When you use
recorder.CheckArguments(Check.IsAny(), Check.IsAny());

TypeMock.NET checks that the argument in values are as expected. even on out and ref parameters.
So instead you should write:
recorder.CheckArguments(new Assign("TypeMock").AndCheck(Check.IsAny()), new Assign(new int[5]));




Perhaps you can see that ‘ref’ arguments have a dual meaning 1. the sent value 2. the return value.

We decided that as long as you don’t check the arguments that meaning will be 2. but once you do it will be 1. this is confusion.

Maybe we should change the default behavior to assign the out values in case of out and ref parameters?
What do you think?
answered by ohad (35.4k points)
0 votes
The way you're handling checked ref- and out parameters is a bit strange. I'd expect recorder.CheckArguments(Check.IsAny(), Check.IsAny()) to check the sent value, but still return the value as if no Check would have happend.

However since this is per design, I think you spend more time on deciding wheter this makes sense or not, therefor it is probably good the way it is for reasons not coming to my mind right now. Nevertheless, you should point out the issue somewhere in your online documentation, as neither https://www.typemock.com/Docs/UserGuide/ ... tural.html nor https://www.typemock.com/Docs/UserGuide/ ... ndOut.html informs about this behavior.
answered by philipp (2.9k points)
0 votes
You are correct that it is hard to find,
The correct place is actually the first link on the Natural TypeMocks™ Handling ref and out Parameters
this points to: Advanced Handling ref and out Parameters.

In any case we have decided to change this behavior in the next release to be give precedence to returning a value then to checking it.
answered by scott (32k points)
0 votes
OK, in this case I have to apologize for my precipant bug report... thanks for your help.
answered by philipp (2.9k points)
...