【问题标题】:FluentAssertions, making sure IEnumerable contains only single elementFluentAssertions,确保 IEnumerable 仅包含单个元素
【发布时间】:2015-10-10 02:28:41
【问题描述】:

我正在编写单元测试,我有一些看起来像这样的东西:

[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?
}

GetFoos() 返回和IEnumberable<Foo>

【问题讨论】:

  • “不起作用”是什么意思?请澄清问题

标签: c# xunit fluent-assertions


【解决方案1】:

OnlyContain 需要一个谓词 -

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

【讨论】:

    【解决方案2】:

    试试这个页面看看它是否有帮助。 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.
    

    【讨论】:

    • 这并不能直接解决问题。
    • 它展示了不同的获得结果的方式,而不是直接给出答案。感谢您的反对票。非常感谢。
    • 我是认真的,也许我应该更具体一点。
    • @EricBowser 您的回答很好,但您需要为初学者添加一个解释,只需一个快速的一行,解释这些行中的一条或多条正在做什么会很棒(排序,我会投票给你,伙计)
    猜你喜欢
    • 2018-01-16
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    相关资源
    最近更新 更多