8

In this question, I found an this answer that seems to be the best way to solve the problem to me.

The provided code assumes that the function that is mocked returns a value:

bool SomeFunc(out ISomeObject o);

However, the object I want to mock has an out function as follows:

void SomeFunc(out ISomeObject o);

The relevant code fragment from the mentioned answer:

public delegate void OutAction<TOut>(out TOut outVal);

public static IReturnsThrows<TMock, TReturn> OutCallback<TMock, TReturn, TOut>(
    this ICallback<TMock, TReturn> mock, OutAction<TOut> action)
    where TMock : class
{
    // ...
}

Void is not a valid type for TReturn. So I believe I would have to somehow adapt this code to get it to work with methods returning void. But how?

Community
  • 1
  • 1
Wilbert
  • 7,251
  • 6
  • 51
  • 91

1 Answers1

17

Maybe you simply need this:

ISomeObject so = new SomeObject(...);
yourMock.Setup(x => x.SomeFunc(out so));

Then when you use yourMock.Object in the code you test, the so instance will "magically" come out as the out parameter.

It is a little non-intuitive ("out is in"), but it works.


Addition: Not sure I understand the scenario. The following complete program works fine:

static class Program
{
  static void Main()
  {
    // test the instance method from 'TestObject', passing in a mock as 'mftbt' argument
    var testObj = new TestObject();

    var myMock = new Mock<IMyFaceToBeTested>();
    IMyArgFace magicalOut = new MyClass();
    myMock.Setup(x => x.MyMethod(out magicalOut)).Returns(true);

    testObj.TestMe(myMock.Object);
  }
}

class TestObject
{
  internal void TestMe(IMyFaceToBeTested mftbt)
  {
    Console.WriteLine("Now code to be tested is running. Calling the method");
    IMyArgFace maf; // not assigned here, out parameter
    bool result = mftbt.MyMethod(out maf);
    Console.WriteLine("Method call completed");
    Console.WriteLine("Return value was: " + result);
    if (maf == null)
    {
      Console.WriteLine("out parameter was set to null");
    }
    else
    {
      Console.WriteLine("out parameter non-null; has runtime type: " + maf.GetType());
    }
  }
}

public interface IMyFaceToBeTested
{
  bool MyMethod(out IMyArgFace maf);
}
public interface IMyArgFace
{
}
class MyClass : IMyArgFace
{
}

Please indicate how your situation is different, by using the names of the classes and interfaces from my example.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • That's not the answer. As in the linked question, the problem is that the code under test will call the function with an out-reference, and I need to set the correct object to the passed-in pointer. – Wilbert Nov 19 '13 at 09:00
  • Have you tried it? It works "magically". I checked the linked thread, and there is [a massively upvoted answer](http://stackoverflow.com/a/3135677/1336654) which suggests the same "solution" as I do. – Jeppe Stig Nielsen Nov 19 '13 at 09:07
  • Yes I did. Please read the comments to that answer. The problem is assignment, and that solution does not solve that problem. – Wilbert Nov 19 '13 at 09:08
  • @Wilbert Assignment how? See my example in the updated answer. – Jeppe Stig Nielsen Nov 19 '13 at 10:44
  • See here: https://gist.github.com/anonymous/7544053. The problem is that within the moq.Setup method, I want to assign some value to the out parameter. The actual pointer to the param is provided by the class under test. Your solution works if the out-pointer is provided by the test. – Wilbert Nov 19 '13 at 11:27
  • 1
    @Wilbert Then substitute the problematic line with: `b_mock.Setup(b => b.Get(out a));` and it works. It seems magical, but it is that simple. – Jeppe Stig Nielsen Nov 19 '13 at 11:36
  • Ok I feel like an idiot now. To think it was so simple all along.. :) Thanks a lot! – Wilbert Nov 19 '13 at 12:35