vanderkleij/Smocks

Unable to handle out params

dlosee opened this issue · 2 comments

If you try something like this (very basic example) it throws an error.

Smock.Run(context =>
{
    int result = 10;
    context.Setup(() => int.TryParse(It.IsAny<string>(), out result)).Returns(true);
}

If you see the inner exception it is basically:
Field 'CLASS+<>c__DisplayClass1.result' is not defined for type 'System.Object

Thanks for reporting! Somehow completely overlooked this so far. Out and ref parameter support is available in version 0.4.3 that's now on nuget.

The behaviour is currently similar to Moq's by the way. That is:

  • Out parameters: mocked methods assign the out parameter the value that the parameter had at the time of setup.
  • Ref parameters: a setup is only matched if ref parameters have the same values as they had during setup. The value isn't changed by the mocked method.

So, in @dlosee's example, calling int.TryParse("AnyStringHere", out outValue); would set outValue to 10, as result was 10 at the time of setup.