Skip to main content
 首页 » 编程设计

c#之如何在 nunit 中测试泛型集合

2024年01月13日30findumars

我怎样才能在 NUnit 中做这样的事情?

class Foo 
{ 
    int Value { get; set; } 
    ... 
} 
... 
ICollection<Foo> someFoos = GetSomeFoos(); 
Expect(List.Map(someFoos).Property("Value"), Has.Some.EqualTo(7)); 

List.Map()只接受 ICollection , 不是 ICollection<T> .

请您参考如下方法:

好吧,你可以转换你的 ICollection<T>实现 ICollection 的东西.数组例如:

ICollection<Foo> someFoos = GetSomeFoos(); 
var array = new Foo[10]; 
someFoos.CopyTo(array); 
Expect(List.Map(array).Property("Value"), Has.Some.EqualTo(7));