如何测试 IEnumerable<SomeClass> 是否具有 MBunit 中 SomeClass 类的所有项目?

StackOverflow https://stackoverflow.com/questions/1082224

  •  22-08-2019
  •  | 
  •  

如何测试 IEnumerable 是否具有 MBunit 中 SomeClass 类的所有项目?

我曾经使用过 Visual Studio 单元测试框架并发现 CollectionAssert.AllAreInstancesOfType 或者检查一下。

但我该如何在 MBunit 中做到这一点呢?

有帮助吗?

解决方案

我在 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));

其他提示

杰夫布朗时,所述加利奥项目的主要开发人员已开通的问题针对该请求。我们要实现几个专用的断言:Assert.ForAllAssert.Exists。他们应该可以在公堂/ MbUnit的(V3.1)的下一个版本,但你将能够在几天下载每日构建(敬请期待)越早使用它们。

修改公堂/ 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));
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top