使用 Func<> 的 FindBy 方法的 Moq 存储库设置

发布于 2024-12-17 03:39:06 字数 1173 浏览 0 评论 0 原文

我是新起订量,一直在努力解决以下问题。

我模拟了一个名为 _mockedThingsList 的列表列表,

我想根据正在测试的服务中提供的 linq 查询来 Moq IRepository 的 FindBy 从这个模拟列表中返回。

我目前所拥有的抛出异常,如下所示。请问有什么问题吗?

mock.Setup(moq => moq.FindBy(It.IsAny<Func<IThing, bool>>()))
            .Returns((enumThingType tp) => _mockedThingsList.Where(x => x.ThingType == tp));

存储库界面如下所示:

interface IRepository<T>
{
    IEnumerable<T> FindAll();

    IEnumerable<T> FindBy(Func<T, bool> predicate);
    void Add(T item);
    void Remove(T item);

    bool Contains(T item);
    int Count { get; }
}

以及将使用此模拟测试的服务

class ThingService
{
    private readonly IRepository<IThing> _repository;

    public ThingService(IRepository<IThing> repository)
    {
        _repository = repository;
    }


    public List<IThing> GetThings1()
    {
        return _repository.FindBy(y => y.ThingType == enumThingType.WhatEver).ToList();
    }

    public List<IThing> GetThings2()
    {
        return _repository.FindBy(y => y.Name == "What ever").ToList();
    }
}

Im new moq and have been struggling with the following.

I have mocked a list of List called _mockedThingsList

I would like to Moq the FindBy of my IRepository to return from this list of mocks based on the a linq query provided in my service that is being tested.

What I have currently have throws an exception and is as follows. What is wrong please?

mock.Setup(moq => moq.FindBy(It.IsAny<Func<IThing, bool>>()))
            .Returns((enumThingType tp) => _mockedThingsList.Where(x => x.ThingType == tp));

The repository interface looks like:

interface IRepository<T>
{
    IEnumerable<T> FindAll();

    IEnumerable<T> FindBy(Func<T, bool> predicate);
    void Add(T item);
    void Remove(T item);

    bool Contains(T item);
    int Count { get; }
}

And the service that would be tested with this mock

class ThingService
{
    private readonly IRepository<IThing> _repository;

    public ThingService(IRepository<IThing> repository)
    {
        _repository = repository;
    }


    public List<IThing> GetThings1()
    {
        return _repository.FindBy(y => y.ThingType == enumThingType.WhatEver).ToList();
    }

    public List<IThing> GetThings2()
    {
        return _repository.FindBy(y => y.Name == "What ever").ToList();
    }
}

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

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

发布评论

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

评论(2

海夕 2024-12-24 03:39:06

我可能缺少一些上下文,但在我看来,你过度嘲笑了。嘲笑名单的目的是什么?您可以轻松返回一个具体列表并将其用于您的测试。

I may be missing some context, but it appears to me that you are over-mocking. What's the purpose of mocking the list? You could easily return a concrete list and use that for your test.

固执像三岁 2024-12-24 03:39:06

我的解释可能是错误的,但 FindBy 的签名返回的是 IEnumerable。而在存根时,您将返回一个委托。我想由于返回类型不匹配,您将面临这个问题。出于测试目的,为什么不从 Returns 方法返回一些正确的列表,即

(moq => moq.FindBy(It.IsAny<Func<IThing, bool>>()))
            .Returns(// return a proper List of type T);

发生了“System.ArgumentException”类型的未处理异常
mscorlib.dll 附加信息:对象类型
'System.Func`2[ThingNamespace.IThing,System.Boolean]' 不能
转换为类型“ThingNamespace.enumThingType”

这就是事情。当您在 FindBy 上声明存根时,您将类型参数指定为:Func。现在,在您的 Returns 方法中,输入参数的类型应该完全相同。尝试以下代码:

Func<IThing, bool> func => (IThing thing) => thing.ThingType == tp;

(moq => moq.FindBy(It.IsAny<Func<IThing, bool>>()))
            .Returns((func) => _mockedThingsList.Where(func));

I might be wrong in my interpretation but the signature of FindBy is returning an IEnumerable. Whereas while stubing, you are returning a delegate. I guess due to this mismatch in the return type, you are facing this issue. For testing purpose, why don't you just return some proper list from Returns method i.e.

(moq => moq.FindBy(It.IsAny<Func<IThing, bool>>()))
            .Returns(// return a proper List of type T);

An unhandled exception of type 'System.ArgumentException' occurred in
mscorlib.dll Additional information: Object of type
'System.Func`2[ThingNamespace.IThing,System.Boolean]' cannot be
converted to type 'ThingNamespace.enumThingType'

Here is the thing. When you declare the stub on FindBy you are specifying the type parameter as : Func. Now in your Returns method the input parameter should exactly be of same type. Try the following code :

Func<IThing, bool> func => (IThing thing) => thing.ThingType == tp;

(moq => moq.FindBy(It.IsAny<Func<IThing, bool>>()))
            .Returns((func) => _mockedThingsList.Where(func));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文