我很高兴使用AutoFixture.Freeze<Mock<IViolationChecker>>()
设置一个类来测试.Verify()
和.Setup()
然而,在我正在研究的课程中,有一大堆IViolationChecker
private readonly IEnumerable<IViolationChecker> _violationCheckers;
我如何使用AutoFixture.Freeze
来定义我的IViolationChecker
列表?我用过
var violationCheckers = AutoFixture.Freeze<Mock<IEnumerable<IViolationChecker>>>();
但我不确定如何写.Verify()
或.Setup()
。
一个选项是冻结集合并为每个项目使用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();
}
//...