Quantcast
Channel: Fluent Assertions
Viewing all articles
Browse latest Browse all 1402

New Post: Collections and its element satisfying a condition

$
0
0

How to assert that a collection has exactly one item of a specified type? At the moment what I can do is:

 

items.Should().HaveCount(1);
items[0].Should().BeOfType<MyType>();

 

There come some questions:

  1. Is it possible to do the same with a one-liner? I'm aware of the HaveElementAt() method, but it requires an object instance to compare with. I can imagine a one-argument overload of:
    object HaveElementAt(int index)
    
    which would return element at that index and expose it to further assertions, like this:
    items.Should().HaveCount(1).And.HaveElementAt(0).OfType<MyType>();
    


  2. In general, is it technically correct for FA to put multiple assertions in one test method?

Viewing all articles
Browse latest Browse all 1402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>