是否可以在组装Returns对象时访问用于调用模拟期望的参数?
这是所涉及对象的存根,鉴于此,我试图模拟一个集合:
Class CollectionValue {
public Id { get; set; }
}
Class Collection {
private List<CollectionValue> AllValues { get; set; }
public List<CollectionValue> GetById(List<int> ids) {
return AllValues.Where(v => ids.Contains(v.Id));
}
}
Run Code Online (Sandbox Code Playgroud)
给定将用于模拟对象的CollectionValues测试列表,如何设置期望来处理CollectionValues列表中ID的每个可能排列,包括组合现有ID和不存在ID的调用?我的问题来自于希望在一次通话中建立所有可能的期望; 如果无法访问原始参数,我可以轻松地设置每次我想在给定调用中测试的确切期望.
这是我希望做的,"???" 表示访问用于调用GetById的参数(符合It.IsAny限制的参数)的方便位置:
CollectionMock.Expect(c => c.GetById(It.IsAny<List<int>>())).Returns(???);
Run Code Online (Sandbox Code Playgroud)
Ben*_*enA 61
从moq 快速入门指南:
// access invocation arguments when returning a value
mock.Setup(x => x.Execute(It.IsAny<string>()))
.Returns((string s) => s.ToLower());
Run Code Online (Sandbox Code Playgroud)
因此,这表明你可以填写你的??? 如
CollectionMock.Expect(c => c.GetById(It.IsAny<List<int>>()))
.Returns((List<int> l) => //Do some stuff with l
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11038 次 |
| 最近记录: |