我有以下代码:
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
未正确填充。
我在这里做错了什么?
答案 0 :(得分:4)
注意:我无法编译您提供的测试..但似乎您需要做的就是:
fixture.Inject(mockRepositoryFactory);
您可以尝试以下方法:
[Fact]
public void TestWithAutoFixtureImperatively()
{
// Fixture setup
var fixture = new Fixture()
.Customize(new AutoFakeItEasyCustomization());
var expectedRecord = fixture.Create<string>();
var boundArgOption = fixture.Create<BoundArgumentOption>();
var repositoryStub = A.Fake<IRepository>();
A.CallTo(() =>
repositoryStub
.Get(boundArgOption))
.Returns(expectedRecord);
var repositoryFactoryStub = A.Fake<IRepositoryFactory>();
A.CallTo(() =>
repositoryFactoryStub
.Create(boundArgOption))
.Returns(repositoryStub);
fixture.Inject(repositoryFactoryStub);
var sut = fixture.Create<HtmlOutputBuilder>();
// Exercise system
string result = sut.BuildReport(boundArgOption);
// Verify outcome
result.Should().Be(expectedRecord);
// Teardown
}
我们注入 IRepositoryFactory
,以便在SUT中传递相同的注入实例。
或者,您也可以将自动混合declaratively与xUnit.net extension一起使用:
[Theory, AutoDomainData]
public void TestWithAutoFixtureDeclaratively(
string expectedRecord,
BoundArgumentOption boundArgOption,
Fake<IRepository> repositoryStub,
[Frozen]Fake<IRepositoryFactory> repositoryFactoryStub,
HtmlOutputBuilder sut)
{
// Fixture setup
A.CallTo(() =>
repositoryStub
.FakedObject
.Get(boundArgOption))
.Returns(expectedRecord);
A.CallTo(() =>
repositoryFactoryStub
.FakedObject
.Create(boundArgOption))
.Returns(repositoryStub.FakedObject);
// Exercise system
string result = sut.BuildReport(boundArgOption);
// Verify outcome
result.Should().Be(expectedRecord);
// Teardown
}
AutoDomainDataAttribute
定义为:
internal class AutoDomainDataAttribute : CompositeDataAttribute
{
internal AutoDomainDataAttribute()
: base(
new AutoDataAttribute(
new Fixture().Customize(
new AutoFakeItEasyCustomization())))
{
}
}