3

How do you Test that a IEnumerable has all items of class SomeClass in MBunit?

I've once used Visual Studio Unit Test Framework and found CollectionAssert.AllAreInstancesOfType or something to check that.

But how do I do it in MBunit?

Abdulsattar Mohammed
  • 10,154
  • 13
  • 52
  • 66
  • 1
    Do you mean "type SomeClass or derived"? Because an IEnumerable cannot have a SomeOtherClass in it unless SomeOtherClass is derived from SomeClass. – John Saunders Jul 04 '09 at 13:43

2 Answers2

1

Jeff Brown, the lead developer of the Gallio project has opened an issue for that request. We are going to implement a few dedicated assertions: Assert.ForAll and Assert.Exists. They should be available in the next release of Gallio/MbUnit (v3.1) but you will be able to use them sooner by downloading the daily build in some days (Stay tuned).

Edit: Starting from Gallio/MbUnit v3.1.213, you can use Assert.ForAll<T>(IEnumerable<T>, Predicate<T>).

[Test]
public void AllMyObjectsShouldBeStrings()
{
  var list = GetThemAll();
  Assert.ForAll(list, x => x.GetType() == typeof(string));
}
Yann Trevin
  • 3,823
  • 1
  • 30
  • 32
0

I didn't see anything in the MBUnit CollectionAssert Class that could help you here

You can easily write your own though (untested).

public class MyCollectionAssert
{
  public void CollectionAssert(IEnumerable source, Predicate<object> assertion)
  {
    foreach(var item in source)
    {
       Assert.IsTrue(assertion(item));
    }
  }

  public void AllAreInstancesOfType(IEnumerable source, Type type)
  {
    return CollectionAssert(source, o => o.GetType() == type);
  }
}

I assuming you actually mean IEnumerable and not IEnumerable<SomeClass> which the compiler enforces the type safety of. To use this extension method call:

MyCollectionAssert.AllAreInstancesOfType(myList, typeof(SomeClass));
bendewey
  • 39,709
  • 13
  • 100
  • 125