使用AutoFixture.Freeze<Mock<IViolationChecker>>()
设置一个类来测试.Verify()
和.Setup()
但是,在我正在上课的班级中,需要枚举IViolationChecker
private readonly IEnumerable<IViolationChecker> _violationCheckers;
如何使用AutoFixture.Freeze
来定义我的IViolationChecker
列表?我用过
var violationCheckers = AutoFixture.Freeze<Mock<IEnumerable<IViolationChecker>>>();
但是我不确定如何写.Verify()
或.Setup()
。
答案 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();
}
//...