最小起订量PARAMS TargetParameterCountException:参数数量不匹配异常不匹配、最小、异常、数量

由网友(杯中残酒)分享简介:以下是我的通用基础信息库接口Following is my generic base repository interfacepublic interface IRepository{IQueryable AllIncluding(params Expression[...

以下是我的通用基础信息库接口

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

和下面是具体的回购

public interface ISdkRepository : IRepository<Sdk>
{
}

现在我想测试一个控制器,采用最小起订量

now I am trying to test a controller, using moq

以下是code,我想测试

Following is the code I am trying to test

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());
    }

使用下面的测试

using following test

[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

不知道为什么?

推荐答案

我想你已经在这里打了一些局限性起订量。它不处理EX pression参数,以及因为它可以通过前pressions作为值本身。有没有办法起订量知道的前pression部分拟解决的,什么是签名的一部分。

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.

另外,我不记得如何起订量手柄PARAMS 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.

您能够创建公开组前pressions作为属性的类?如果是这样有可能改变AllIncluding的签名,并告诉最小起订量,以匹配任何该类的实例。

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.

更新

目前应答,这是一种限制,但现在是可能的时间。一看便知通过奥列克Lytvyn

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

阅读全文

相关推荐

最新文章