AutoFixture.Freeze IE模拟

时间:2019-02-19 22:40:58

标签: c# moq xunit autofixture

使用AutoFixture.Freeze<Mock<IViolationChecker>>()设置一个类来测试.Verify().Setup()

时,我感到很舒服

但是,在我正在上课的班级中,需要枚举IViolationChecker

private readonly IEnumerable<IViolationChecker> _violationCheckers;

如何使用AutoFixture.Freeze来定义我的IViolationChecker列表?我用过

var violationCheckers = AutoFixture.Freeze<Mock<IEnumerable<IViolationChecker>>>();

但是我不确定如何写.Verify().Setup()

1 个答案:

答案 0 :(得分:1)

一种选择是冻结集合并为每个项目使用Mock.Get以获得用于设置和验证的模拟。

//Arrange
var violationCheckers = AutoFixture.Freeze<IEnumerable<IViolationChecker>>();
var mocks = violationCheckers.Select(item => Mock.Get(item));
foreach (var mock in mocks) {
    mock.Setup(_ => _.SomethingHere()).Verifiable(); //setup mock as desired.
}

//...

//Act
//...exercise member under test

//Assert
foreach(var mock in mocks) {
    mock.Verify(_ => /* some predicate*/, Times.Once);
    //OR
    //mock.Verify();
}

//...