简体   繁体   English

如果使用LINQ查询,是否需要对单元测试进行排序?

[英]If using LINQ For Queries, Do We Need to Unit-Test Sorting?

I have the following two methods on an interface for my service layer: 我的服务层的接口上有以下两种方法:

ICollection<T> FindAll<T>(Expression<Func<T, bool>> predicate) where T : Post;
ICollection<T> FindAll<T,TKey>(Expression<Func<T, bool>> predicate, OrderingOptions<T,TKey> orderingOptions) where T : Post;

They are extremely similar. 它们极为相似。

The first one (basically) does this: 第一个(基本上)是这样做的:

return repository
   .Find()
   .OfType<T>()
   .Where(predicate)
   .Take(maxRows)
   .ToList();

The second one (basically) does this: 第二个(基本上)执行此操作:

return repository
   .Find()
   .OfType<T>()
   .Where(predicate)
   .WithOrdering(orderingOptions)
   .Take(maxRows)
   .ToList();

WithOrdering is a pipe extension method i created: WithOrdering是我创建的管道扩展方法:

public static IOrderedQueryable<T> WithOrdering<T,TKey>(this IQueryable<T> source, OrderingOptions<T,TKey> postOrderingOptions)
        {
            return postOrderingOptions.SortAscending
                       ? source.OrderBy(postOrderingOptions.OrderingKey)
                       : source.OrderByDescending(postOrderingOptions.OrderingKey);

        }

OrderingOptions<T,TKey> is a generic class i implemented to provide fluent ordering. OrderingOptions<T,TKey>是我实现以提供流畅排序的通用类。

So, that being said - i have numerous unit tests for the first method (ensure collection is returned, ensure null is returned for an empty collection, ensure filtered correctly, ensure max rows is returned, etc). 所以,话虽这么说-我对第一种方法有很多单元测试(确保返回了集合,确保为空集合返回了null,确保正确过滤,确保返回了最大行,等等)。

But my question is - how do i test the second method? 但是我的问题是-我如何测试第二种方法? Do i need two? 我需要两个吗? Given that the only thing that is different is that im using the .OrderBy (or .OrderByDescending) methods. 鉴于唯一不同的是im使用.OrderBy(或.OrderByDescending)方法。

If im testing that, am i simply testing the LINQ framework? 如果我正在测试,我是否只是在测试LINQ框架?

If not, how can i even test this? 如果没有,我什至可以对此进行测试?

Couple of (possibly important) points: 几个要点(可能很重要):

  1. This are methods on a service layer , which retrieve IQueryable's on a repository , that is implemented with Entity Framework 4.0 这是服务层上的方法,该服务层在使用Entity Framework 4.0实现的存储库中检索IQueryable。
  2. For my unit-tests, the service layer is injected (via StructureMap) a mock repository, but i also toggle this to the real repository manually when i want to do integration testing 对于我的单元测试, 服务层 (通过StructureMap)注入了一个模拟存储库,但是当我想进行集成测试时,我也手动将其切换到真实存储库

Guidance/best practices would be highly appreciated. 指导/最佳做法将不胜感激。

Thanks! 谢谢!

You need to unit-test the second method just to make sure it works. 您需要对第二种方法进行单元测试,以确保它可以工作。 If you refactor something that changes how it works and fix the first one, how will you know if you forgot to fix the second one? 如果您重构某些东西来改变它的工作方式并修复第一个,那么您如何知道是否忘记修复第二个呢? If you refactor WithOrdering , how will you know that things still work? 如果重构WithOrdering ,您将如何知道事情仍然有效? I would have a unit test to test ascending and one to test descending. 我将有一个单元测试来测试升序,一个单元测试来测试降序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM