5

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 IEnumerable<Foo>

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2958542
  • 331
  • 1
  • 8
  • 18

3 Answers3

7

OnlyContain expects a predicate -

 GetFoos().Should().OnlyContain(x => _foo1.Equals(x));
George Simms
  • 3,930
  • 4
  • 21
  • 35
2

With 6.7.0 version of FluentAssertions you can use :

_sut.GetFoos()
    .Should().ContainSingle()
    .Which.Should().Be(_foo1);
Sophia
  • 21
  • 1
-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.
  • This doesn't directly address the question. – George Simms Jul 20 '15 at 20:37
  • It shows different ways of getting results without giving the answer directly. Thanks for the down vote. Much appreciated. – Eric Bowser Jul 21 '15 at 02:00
  • Im serious though, maybe I should have been more specific. – Eric Bowser Jul 21 '15 at 02:19
  • @EricBowser your answer was good but you need to add an explanation for the uninitiated just a quick one liner explaining what one or more of those lines are doing would have been great (sort that and I will up vote you buddy) – Trevor Apr 03 '17 at 12:47