假设我有一个Service
对有依赖关系的TimeProvider
。
FakeTimeProvider
我现在要使用 AutoFixture 和 Moq来设置我的测试的使用情况:
public class FakeTimeProviderCustomization : ICustomization {
public void Customize(IFixture fixture) {
fixture.Customize<TimeProvider>
(c => c.FromFactory(() => new FakeTimeProvider()));
}
}
public class MoqAutoDataAttribute : AutoDataAttribute {
public MoqAutoDataAttribute()
: base(() => new Fixture()
.Customize(new AutoMoqCustomization())
.Customize(new FakeTimeProviderCustomization())
) {
}
}
[Theory, MoqAutoData]
public async Task Test_TimeRelatedStuff(
[Frozen] TimeProvider timeProvider,
Service sut) {
...
var fakeTimeProvider = timeProvider as FakeTimeProvider;
...
我可以避免FakeTimeProvider
每次都必须进行的转换吗?