1

I am looking for a way to change the value of a Parameter for a Mock function with FakeItEasy.

I have something like:

var objParam = new ObjParam();
objParam.SomeIntValue = 0;

A.CallTo(() => iClass.Func(objParam)).WithAnyArguments().DoesNothing();

and I want to set the value of objParam for after the function is called, for example like this:

objParam.SomeIntValue += 1;

objParam is neither a ref nor a out Parameter, it is an object. Is there a way to do this with FakeItEasy?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Kiroul
  • 465
  • 2
  • 9

1 Answers1

3

Capture the passed argument and perform the desired custom functionality

//Arrange
IClass iClass = A.Fake<IClass>();

A.CallTo(() => iClass.Func(A<ObjParam>._))
    .Invokes((ObjParam arg) => arg.SomeIntValue += 1);

var objParam = new ObjParam();
objParam.SomeIntValue = 0;

//Act
iClass.Func(objParam);

//Assert
objParam.SomeIntValue.Should().Be(1);

Reference Invoking Custom Code

Nkosi
  • 235,767
  • 35
  • 427
  • 472