【问题标题】:FluentAssertions Comparing two list of different typeFluentAssertions 比较两个不同类型的列表
【发布时间】:2014-11-12 02:46:45
【问题描述】:

我想知道是否有办法比较两个不同类型的列表。我找到了这个方法:

        public AndConstraint<TAssertions> Equal(IEnumerable<T> expectation, Func<T, T, bool> predicate, string because = "", params object[] reasonArgs)
        {
          this.AssertSubjectEquality<T>((IEnumerable) expectation, predicate, because, reasonArgs);
          return new AndConstraint<TAssertions>((TAssertions) this);
        }

我正在寻找类似的东西:

    public AndConstraint<TAssertions> Equal<U>(IEnumerable<T> expectation, Func<T, U, bool> predicate, string because = "", params object[] reasonArgs)
    {
      this.AssertSubjectEquality<T,U>((IEnumerable) expectation, predicate, because, reasonArgs);
      return new AndConstraint<TAssertions>((TAssertions) this);
    }

我尝试制作扩展方法,但方法 AssertSubjectEquality 受到保护,不支持第二种类型。

【问题讨论】:

  • 不确定我是否完全理解您想要什么。您有两个不同泛型类型的不同列表,并且您想针对每个条目按顺序测试谓词?所以也许是这样的? list1.Zip(list2, (a, b) =&gt; new { A = a, B = b }).Should().Equal(zippedInput =&gt; zippedInput.A == "something" &amp;&amp; zippedInput.B == "somethingElse");?除此之外,由于 FluentAssertions 是开源的,您可以随时下载并添加到其中以供您使用。我已经完成了这项工作,并且没有遇到任何重大问题。
  • 你明白了,你的代码稍作修改就可以工作list.Zip(monitoringObjects, (a, b) =&gt; new { A = a, B = b }).Should().Contain(x=&gt; x.A.Id == x.B.Id);虽然它不是很有效,因为我使用的是Contain,它适用于无序列表。也许@DennisDomen 可能有一个建议。

标签: c# fluent-assertions


【解决方案1】:

如果不复制 CollectionAssertions 类的 AssertSubjectEqualityAssertCollectionsHaveSameCount 方法,您将无法做到这一点。

相反,我建议你 fork 存储库并向我发送一个 Pull Request,在其中你将这两个方法的通用参数从 &lt;T&gt; 更改为 &lt;T, U&gt;,并将你的方法添加到 GenericCollectionAssertions。我会把它作为 v3.3 的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-25
    • 2019-07-14
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多