我正在使用NUnit 2.5.7。我想测试一个特定类的自定义对象集合是否包含某些对象,基于该类的一个属性。
例如,一个编造的例子...
public class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
}
// ...
public List<Person> GetFavouritePeople()
{
List<Person> favouritePeople = new List<Person>();
favouritePeople.Add(new Person("joe"));
favouritePeople.Add(new Person("fred"));
favouritePeople.Add(new Person("jenny"));
return favouritePeople;
}
// ...
[Test]
public GetFavouritePeople()
{
List<Person> people = GetFavouritePeople();
// What I'd like to test, but not sure how to do it...
Assert.Contains(Name="joe", people);
Assert.Contains(Name="fred", people);
Assert.Contains(Name="jenny", people);
}
尽管在这个例子中很简单,但我不想为每个Person创建模拟对象并在断言中使用它们... 我只想基于特定属性(例如,在这个例子中是Name)进行检查。
Assert.All()
和Assert.Contains()
: https://github.com/xunit/assert.xunit/blob/main/CollectionAsserts.cs - Aasmund EldhusetAssert.Contains(result.Value.Members, x => x.Consumption == null);
- Enrico