I have the following code to set up some response from a mock client (some parts of the code are omitted for brevity):
IFixture fixture = new Fixture().Customize(new AutoMoqCustomization()
{ ConfigureMembers = true, GenerateDelegates = true });
CustomNetworkCall<ResponseObject> GetNewResponse()
{
var response = fixture.Create<ResponseObject>();
fixture.Customize<ResponseObject>(ro => ro.FromFactory(() =>
{
response.DataList.AddRange(fixture.CreateMany<DataObject>(random.Next(3, 10)));
return response;
})
.With(item => item.Id, Guid.NewGuid().ToString())
);
return fixture.Create<CustomNetworkCall<ResponseObject>>();
}
//setup for mock code
clientMock.Setup(client =>
client.GetResponseAsync(
It.IsAny<IdDescription>(),
It.IsAny<DataValidation>(),
It.IsAny<DateTime?>(),
It.IsAny<CancellationToken>()))
.Returns(GetNewResponse());
I use the mocked out method to get a new instance but I always get the firstly created ResponseObject
instance with the same Id
:
try
{
//I always get the same instance!!!
responseObject = await basicClient.GetResponseAsync(new(){Id = Guid.NewGuid()});
}
catch (Exception e)
{
Logger.LogError(e, e.Message);
}
Unfortunately the documentation is "quite" empty on this topic.
What should I change to get a new instance every time I call?
will return the same instance provided when
GetNewResponse()
is invoked.If you want a different instance per call then refactor the
.Returns
to invoke a delegate