Moq Params TargetParameterCountException:参数计数不匹配异

时间:2023-05-07
本文介绍了Moq Params TargetParameterCountException:参数计数不匹配异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

以下是我的通用基础存储库接口

Following is my generic base repository interface

public interface IRepository<T>
{
    IQueryable<T> AllIncluding(params Expression<Func<T, 
                               object>>[] includeProperties);
}

我的实体

public class Sdk 
{
    public Sdk()
    {
       this.Identifier = Guid.NewGuid().ToString();
    }

    public virtual ICollection<Resource> AccessibleResources { get; set; }

    public string Identifier { get; set; }    
}

以下是具体的repo

public interface ISdkRepository : IRepository<Sdk>
{
}

现在我正在尝试使用最小起订量测试控制器

now I am trying to test a controller, using moq

以下是我要测试的代码

public ActionResult GetResources(string clientId) {
        var sdkObject = sdkRepository
                           .AllIncluding(k => k.AccessibleResources)
                           .SingleOrDefault(k => k.Identifier == clientId);
        if (sdkObject == null)
            throw new ApplicationException("Sdk Not Found");
        return Json(sdkObject.AccessibleResources.ToList());
    }

使用以下测试

[Test]
public void Can_Get_GetResources()
{
    var cid = Guid.NewGuid().ToString();
    var mockRepo = new Moq.Mock<ISdkRepository>();
    var sdks = new HashSet<Sdk>()
    {
        new Sdk()
        {
            Identifier = cid,
            AccessibleResources = new HashSet<Resource>()
            {
                new Resource()
                {
                    Id = Guid.NewGuid(),
                    Description = "This is sdk"
                }
            }
        }
    };
    mockRepo.Setup(k => k.
        AllIncluding(It.IsAny<Expression<Func<Sdk,object>>[]>()))
                       .Returns(sdks.AsQueryable);
    var sdkCtrl = new SdksController(mockRepo.Object);
    var returnedJson=sdkCtrl.GetResources(cid);
    returnedJson.ToString();
}

它正在抛出:

System.Reflection.TargetParameterCountException : 参数计数不匹配

System.Reflection.TargetParameterCountException : Parameter count mismatch

不知道为什么?

推荐答案

我认为您在使用 Moq 时遇到了一些限制.它不能很好地处理表达式参数,因为它可以将表达式作为值本身传递.Moq 无法知道要解析表达式的哪一部分以及签名的哪一部分.

I think you've hit some limitations here with Moq. It doesn't handle expression parameters well because it can be passed expressions as values itself. There's no way for Moq to know what part of the expression is intended to be resolved and what is part of the signature.

另外,我不记得 Moq 处理参数 xx[] 的能力如何,但很可能您在这里遇到了两个问题.

Also, I can't remember how well Moq handles params xx[] but it's quite possible you have a combination of two problems here.

您是否能够创建一个将表达式集公开为属性的类?如果是这样,则可以更改 AllIncluding 的签名并告诉 Moq 匹配该类的任何实例.

Are you able to create a class that exposes the set of expressions as a property? If so it might be possible to change the signature of AllIncluding and tell Moq to match on any instance of that class.

更新

在回答时这是一个限制,但现在可以了.查看 Oleksandr Lytvyn 的答案

At the time of answering this was a limitation but is now possible. See the answer by Oleksandr Lytvyn

这篇关于Moq Params TargetParameterCountException:参数计数不匹配异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 .Net Core 测试中模拟 UserManager? 下一篇:模拟不是接口的类

相关文章

最新文章