C# 如何在nunit中测试泛型集合?

C# 如何在nunit中测试泛型集合?,c#,.net,nunit,C#,.net,Nunit,我怎么能在努尼特做这样的事 class Foo { int Value { get; set; } ... } ... ICollection<Foo> someFoos = GetSomeFoos(); Expect(List.Map(someFoos).Property("Value"), Has.Some.EqualTo(7)); class-Foo { int值{get;set;} ... } ... ICollection someFoos=GetSome

我怎么能在努尼特做这样的事

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

List.Map()
只接受
ICollection
,而不接受
ICollection

好的,您可以在概念上使用linq到对象扩展,例如:


Expect(someAs.Count(),Has.Some.EqualTo(7))

好的,您可以在概念上使用linq到对象扩展,比如:


Expect(someAs.Count(),Has.Some.EqualTo(7))

如果您尝试这样的方法,会怎么样:

List<Foo> someFoos = GetSomeFoos();
List someFoos=GetSomeFoos();

由于
List
实现了
ICollection
接口。

如果您尝试类似的方法,会怎么样:

List<Foo> someFoos = GetSomeFoos();
List someFoos=GetSomeFoos();

由于
List
实现了
ICollection
接口。

那么,您可以将
ICollection
转换为实现
ICollection
的内容。数组,例如:

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

好吧,您可以将
ICollection
转换为实现
ICollection
的东西。数组,例如:

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

我不想测试someFoos中的元素数量,但有些元素的属性值等于7。好的,这只是一个示例。您可以很容易地使用不同的扩展,例如Any操作符。someAs.Any(a=>a.Value==7)我不想测试someFoos中的元素数量,但有些元素的属性值等于7。好的,这只是一个例子。您可以很容易地使用不同的扩展,例如Any操作符。someAs.Any(a=>a.Value==7)