【问题标题】:Working LINQ query does not work in unit test工作 LINQ 查询在单元测试中不起作用
【发布时间】:2015-01-28 16:35:27
【问题描述】:

我有一个与此非常相似的查询:

var result = context.EntityRole
   .Where(er => er.EntityType.EntityTypeId == entityTypeIdParameter
                && er.Entity.SomeItems.Any(item => item.ItemId == itemIdParameter))
   .ToList()
   .Distinct(customItemComparer)
   .OrderBy(er => er.Id)
   .ThenByDescending(er => er.IsApproved)
   .ToList();

customItemComparer 类似于:

public class CustomItemComparer : IEqualityComparer<EntityRole>
{
   public bool Equals(EntityRole x, EntityRole y)
   {
      if (ReferenceEquals(x, y)) return true;

      if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) return false;

      return x.Property1 == y.Property1
             && x.Property2 == y.Property2
             && x.Property3 == y.Property3;
   }

   public int GetHashCode(EntityRole obj)
   {
      if (ReferenceEquals(obj, null)) return 0;

      var hasProperty1 = obj.Property1.GetHashCode();
      var hasProperty2 = obj.Property2.GetHashCode();
      var hasProperty3 = obj.Property3.GetHashCode();

      return hasProperyt1 ^ hasProperty2 ^ hasProperty3;
   }
}

我遇到的问题是,当我运行应用程序时,我得到了预期的结果,并且显然所有不同的场景都可以完美运行,但是当我尝试对其进行单元测试时,即使列表包含超过一个对象,其属性 1、2 和 3 不同。

我的单元测试看起来像这样,我们使用的是最小起订量,为简洁起见删除了其他属性:

var roles = new List<EntityRole>
{
   new EntityRole
   {
      Property1 = true,
      Property2 = 5,
      Property3 = "something"
   },
   new EntityRole
   {
      Property1 = true,
      Property2 = 9,
      Property3 = "something"
   },
   new EntityRole
   {
      Property1 = false,
      Property2 = 5,
      Property3 = "something"
   },
   new EntityRole
   {
      Property1 = true,
      Property2 = 5,
      Property3 = "something else"
   }
}

contextMock.Setup(c => c.EntityRole).Returns(roles.AsQueryable);

var sut = new SubjectUnderTest();
sut.MethodWhereQueryIsExecuted();

//some code to verify results

所以就像我说的,即使列表中没有两个相同的对象,查询总是返回第一个。

此外,如果我在 CustomItemComparer 中设置断点,则在运行应用程序时会停止执行,但在调试测试时不会停止。

那么确切的问题是,为什么 Distinct 在应用程序运行时运行良好,而在单元测试运行时却无法运行?

【问题讨论】:

  • 尝试删除ToList之后的所有内容?有多少项目?也许Where 已经将它们过滤掉了......
  • 您应该真正构建该查询,以便所有这些操作都在数据库上执行,而不是在您的应用程序中。
  • 答案应该是 - 测试不评估查询(例如缺少 .ToList()),但如果没有显示代码则很难说。
  • 看起来你最后一行代码contextMock.Setup(c =&gt; c.EntityRole).Returns(roles.AsQueryable); 没有任何意义。 Setup 方法应该模拟一些对象的方法,Returns 应该从这个方法返回一些假结果。我没有看到,您的 contextMock 在哪里调用 db 查询或使用该查询的方法

标签: c# linq unit-testing moq


【解决方案1】:

我在使用带有 Distinct 的 MOQ 时遇到了同样的问题,它在生产中运行良好,但在测试中却惨遭失败。显然,对于 Mock.Object 类型的对象,它似乎无法按预期工作。

在我的场景中,我选择使用来自 MoreLINQ 的 DistinctBy 扩展。如果您只想使用 Linq,那么您可以使用 GroupBy 并取每个组中的第一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    相关资源
    最近更新 更多