FluentAssertions, making sure IEnumerable contains only single element

狂风中的少年 提交于 2019-12-24 05:42:51

问题


I am writing unit tests and I have something that looks like this:

[Fact]
public void GetFoos_only_gets_foo1()
{
    _foo1.included = true; //Foo object
    _foo2.included = false; //Foo object
    _sut.GetFoos().Should().OnlyContain(_foo1); // this doesn't work, is there a specific syntax to use?
}

And GetFoos() returns and IEnumberable<Foo>


回答1:


OnlyContain expects a predicate -

 GetFoos().Should().OnlyContain(x => _foo1.Equals(x));



回答2:


Try this page as see if it helps. https://github.com/dennisdoomen/fluentassertions/wiki

collection.Should().ContainSingle();
collection.Should().ContainSingle(x => x > 3);
collection.Should().Contain(8).And.HaveElementAt(2, 5).And.NotBeSubsetOf(new[] {11, 56});
collection.Should().Contain(x => x > 3); 
collection.Should().Contain(collection, 5, 6); // It should contain the original items, plus 5 and 6.


来源:https://stackoverflow.com/questions/31525434/fluentassertions-making-sure-ienumerable-contains-only-single-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!