Home > OS >  How to setup method out parameter to return mock in c#
How to setup method out parameter to return mock in c#

Time:01-20

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.

CodePudding user response:

I believe this should work:

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