AutoFixture 和 AutoMoq 在自定义后总是 returns 同一个实例

AutoFixture with AutoMoq always returns the same instance after Customization

我有以下代码来设置来自模拟客户端的一些响应(为简洁起见省略了部分代码):

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());
           

我使用模拟方法获取新实例,但我总是获取第一个创建的 ResponseObject 具有相同 Id:

的实例
try
{
    //I always get the same instance!!!
    responseObject = await basicClient.GetResponseAsync(new(){Id = Guid.NewGuid()}); 
}
catch (Exception e)
{
    Logger.LogError(e, e.Message);
}

不幸的是 documentation 在这个话题上“相当”空洞。

每次调用时我应该更改什么以获得新实例

//...
.Returns(GetNewResponse());

将return调用GetNewResponse()时提供的相同实例。

如果您想要每次调用不同的实例,请重构 .Returns 以调用委托

//...
.Returns(() => GetNewResponse()); //<-- note the function in .Returns