我有以下代码:
var boundArgument = new BoundArgumentOption
{
PatientId = patientId
};
var mockRepositoryFactory = A.Fake<IRepositoryFactory>();
var sut = new HtmlOutputBuilder(mockRepositoryFactory);
var patientRecord = // Some record;
var mockRepository = A.Fake<IRepository>();
A.CallTo(() => mockRepository.Get(boundArgument)).Returns(patientRecord);
A.CallTo(() => mockRepositoryFactory.Create(boundArgument)).Returns(mockRepository);
string actualResult = sut.BuildReport(boundArgument);
actualResult.Should().Be(expectedHtmlContent);
并且通过了测试。
然后我尝试使用 AutoFixture,如下所示:
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
var boundArgument = fixture.Create<BoundArgumentOption>();
var mockRepositoryFactory = A.Fake<IRepositoryFactory>();
fixture.Freeze(mockRepositoryFactory);
var sut = fixture.Create<HtmlOutputBuilder>();
var patientRecord = //Some record;
boundArgument.PatientId = patientId;
var mockRepository = A.Fake<IRepository>();
A.CallTo(() => mockRepository.Get(boundArgument)).Returns(patientRecord);
A.CallTo(() => mockRepositoryFactory.Create(boundArgument)).Returns(mockRepository);
string actualResult = sut.BuildReport(boundArgument);
actualResult.Should().Be(expectedHtmlContent);
失败了。特别是,patientRecord
在第二种情况下,没有正确填充。
我在这里做错了什么?