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

发布于 2024-09-25 19:32:13 字数 1687 浏览 0 评论 0原文

我的服务层接口上有以下两种方法:

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;

它们非常相似。

第一个(基本上)执行此操作:

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

第二个(基本上)执行此操作:

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

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 是通用的我实现的类是为了提供流畅的排序。

因此,话虽这么说,我对第一个方法进行了大量的单元测试(确保返回集合、确保空集合返回 null、确保正确过滤、确保返回最大行数等)。

但我的问题是 - 我如何测试第二种方法?我需要两个吗?鉴于唯一不同的是我使用 .OrderBy (或 .OrderByDescending)方法。

如果我测试它,我只是测试 LINQ 框架吗?

如果没有,我该如何测试呢?

几个(可能很重要)点:

  1. 这是服务层上的方法,它们在存储库上检索 IQueryable,这是使用 Entity Framework 4.0 实现的
  2. 对于我的单元测试, 服务层被注入(通过StructureMap)一个模拟存储库,但当我想要进行集成测试时,我也会手动将其切换到真实存储库

指导/最佳实践是高度赞赏。

谢谢!

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:

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.

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).

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.

If im testing that, am i simply testing the LINQ framework?

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
  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

Guidance/best practices would be highly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2024-10-02 19:32:13

您需要对第二种方法进行单元测试以确保它有效。如果你重构一些改变其工作方式的东西并修复第一个,你怎么知道你是否忘记修复第二个?如果您重构 WithOrdering,您如何知道事情仍然有效?我将有一个单元测试来测试升序,一个单元测试来测试降序。

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? I would have a unit test to test ascending and one to test descending.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文