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?
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?
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));
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));
}