Search code examples
c#moqout

How to setup method out parameter to return mock in c#


I have service mock, which calls method, where one of parameters is out parameter. How is it possible to out mocked object as this parameter, because I need to set up this mock further.

var randomObjectMock = new Mock<ISmth>(MockBehavior.Strict);
mock.Setup(x => x.DoSomething(out randomObjectMock);
ISmth randomObject;
var randomObjectMock = new Mock<ISmth>(MockBehavior.Strict);
mock.Setup(x => x.DoSomething(out randomObjectMock);`

Initialization randomObject is not an option.

Mock<ISmth > randomObject;
mock.Setup(x => x.DoSomething(out randomObjectMock.Object);

This also not an option.


Solution

  • I believe this should work:

    var randomObjectMock = new Mock<ISmth>(MockBehavior.Strict);
    var smth = randomObjectMock.Object;
    mock.Setup(x => x.DoSomething(out smth));