您如何测试 IEnumerable 在 MBunit 中具有 SomeClass 类的所有项目?
我曾经使用过 Visual Studio 单元测试框架并找到CollectionAssert.AllAreInstancesOfType
了一些东西来检查它。
但是我如何在 MBunit 中做到这一点?
您如何测试 IEnumerable 在 MBunit 中具有 SomeClass 类的所有项目?
我曾经使用过 Visual Studio 单元测试框架并找到CollectionAssert.AllAreInstancesOfType
了一些东西来检查它。
但是我如何在 MBunit 中做到这一点?
Gallio 项目的首席开发人员Jeff Brown已针对该请求打开了一个问题。我们将实现一些专用断言:Assert.ForAll
和Assert.Exists
. 它们应该在 Gallio/MbUnit (v3.1) 的下一个版本中可用,但是您可以通过在某些日子内下载每日构建来更快地使用它们(敬请期待)。
编辑:
从Gallio/MbUnit v3.1.213 开始,您可以使用Assert.ForAll<T>(IEnumerable<T>, Predicate<T>)
.
[Test]
public void AllMyObjectsShouldBeStrings()
{
var list = GetThemAll();
Assert.ForAll(list, x => x.GetType() == typeof(string));
}
我在 MBUnit 类中没有看到任何CollectionAssert
可以帮助你的东西
您可以轻松编写自己的内容(未经测试)。
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);
}
}
我假设您实际上是指 IEnumerable 而不是IEnumerable<SomeClass>
编译器强制执行类型安全的。要使用此扩展方法调用:
MyCollectionAssert.AllAreInstancesOfType(myList, typeof(SomeClass));