Mock object returning a list of mocks with Moq

自古美人都是妖i 提交于 2019-12-03 13:29:45

In your _cleanUpTaskFactory mock you could simply do something like the following:

var mocks = new List<Mock<ITask>>();
for(var i = 0; i < 10; i++){
    var mock = new Mock<ITask>();
    mock.Setup(t => t.Execute()).Verifiable();
    mocks.Add(mock);
}

_cleanUpTaskFactoryMock.Setup(f => f.GetTasks()).Returns(mocks.Select(m => m.Object).Tolist());

Now make sure to keep a reference to the mocks list, and when you done with your testing you iterate over all the mocks and call Verify() like so:

mocks.ForEach(m => m.Verify());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!