0

I'm probably missing something about CollectionAssert.AllItemsAreNotNull

Verifies that all items in the specified collection are not null. The assertion fails if any element is null.

var foo = new List<string>();

//Passes - why?
CollectionAssert.AllItemsAreNotNull(foo);

//Sanity check
var isnull = foo.FirstOrDefault(); 
//Yes indeed the first item in the collection is null   
Assert.IsTrue(isnull == null, "isnull is not null");

//Now I'm getting what I expected (fail)
Assert.IsTrue(foo.Any(), "foo has no items");

Welp...thnx!


Update

Thanks to all for the clarifications/explanations - completely makes sense. I had a different/opinionated/wrong interpretation (aka "NullOrEmpty").

For those who want to dig a bit more, this reference should fully clarify:

public static void AllItemsAreNotNull(ICollection collection, string message, params object[] parameters)
{
    Assert.CheckParameterNotNull(collection, "CollectionAssert.AllItemsAreNotNull", "collection", string.Empty);
    foreach (object current in collection)
    {
        if (current == null)
        {
            Assert.HandleFail("CollectionAssert.AllItemsAreNotNull", message, parameters);
        }
    }
}

Hth and thnx again!

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • 3
    Why are you expecting the first assertion to fail? There are no items in the collection, so that means all items in the collection are not `null`. Also, the first item in the collection isn't `null`, because there is no `First` in the collection, it returns the default value for string (`null`). – Evan Trimboli Jun 28 '18 at 00:05
  • 1
    `The assertion fails if any element is null.` Are any elements `null`? No, so it doesn't fail. `//Yes indeed the first item in the collection is null` -> this comment should be changed to `//Yes indeed the first item in the collection is null (or the collection is empty)`. – mjwills Jun 28 '18 at 00:07
  • @EvanTrimboli - ok, I get it. Though _"so that means all items in the collection are not `null`_ - what `item/s`? (Is where I'm coming from). Tnx! – EdSF Jun 28 '18 at 00:38
  • By virtue of the collection being empty, all the items in the collection can't match any condition. Are any items in the collection null? No, because there aren't any items to be null. – Evan Trimboli Jun 28 '18 at 00:48

0 Answers0