使用 Moq 实现 IQueryable 的模拟类

Mocking classes that implement IQueryable with Moq(使用 Moq 实现 IQueryable 的模拟类)
本文介绍了使用 Moq 实现 IQueryable 的模拟类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一个晚上试图模拟一个实现 IQueryable 的对象:

I spent an evening trying to mock an object that implements IQueryable:

public interface IRepo<T> : IQueryable<T>
{
}

我能想到的最好的方法是这样的:

The best I could come up with is something like this:

var items = new Item[] {}.AsQueryable();

var repo = new Mock<IRepo>();
repo.Setup(r => r.GetEnumerator()).Returns(items.GetEnumerator());
repo.Setup(r => r.Provider).Returns(items.Provider);
repo.Setup(r => r.ElementType).Returns(items.ElementType);
repo.Setup(r => r.Expression).Returns(items.Expression);

有没有更简洁的方法来做同样的事情?在 IRepo 中公开一个返回 IQueryable 的属性/方法会更容易,并且像这样简单地模拟:

Is there a more concise way to do the same? It would be easier to expose a property/method in IRepo that returns IQueryable and the simply mock like this:

repo.Setup(r => r.GetItems()).Returns(new Items[]{ }.AsQueryable());

但这不是我想做的=)

推荐答案

这并不是什么新鲜事,只是一种更简洁的方式.我也有存储库,其中存储库本身也是 IQueryable,所以我需要同样的东西.我基本上只是在我的测试项目的根级别将您的代码放入这样的扩展方法中,以使其可用于所有测试:

This is nothing new, just a cleaner way of doing it. I also have repositories where the repository itself is also an IQueryable, so I needed the same thing. I basically just put your code into an extension method like this at the root level of my test project, to make it available to all tests:

public static class MockExtensions
{
    public static void SetupIQueryable<T>(this Mock<T> mock, IQueryable queryable)
        where T: class, IQueryable
    {
        mock.Setup(r => r.GetEnumerator()).Returns(queryable.GetEnumerator());
        mock.Setup(r => r.Provider).Returns(queryable.Provider);
        mock.Setup(r => r.ElementType).Returns(queryable.ElementType);
        mock.Setup(r => r.Expression).Returns(queryable.Expression);
    }
}

这基本上只是提供了可重用性,因为您可能希望在多个测试中执行此操作,并且在每个测试中它都使意图清晰且混乱最小化.:)

This basically just offers reusability, since you're likely to want to do this in several tests, and in each test it makes the intention clear and the mess minimal. :)

这篇关于使用 Moq 实现 IQueryable 的模拟类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)