简体   繁体   中英

How does one test generic collections in nunit?

How can I do something like this in NUnit?

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

List.Map() only accepts ICollection , not ICollection<T> .

well, you could conceptually use linq to objects extensions, something like:

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

What if you tried something like this instead:

List<Foo> someFoos = GetSomeFoos();

as List<T> does implement the ICollection interface.

Well, you could convert your ICollection<T> to something that implements ICollection . Array for instance:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM