gpt4 book ai didi

c# - 起订量参数 TargetParameterCountException : Parameter count mismatch Exception

转载 作者:可可西里 更新时间:2023-11-01 08:30:46 24 4
gpt4 key购买 nike

以下是我的通用基础存储库界面

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

现在我正在尝试使用最小起订量测试 Controller

下面是我要测试的代码

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 : Parameter count mismatch

不知道为什么?

最佳答案

虽然有一个标记为已接受的答案,但我相信有一种方法可以正确模拟您的存储库。

代替

mockRepo.Setup(k => k.AllIncluding(It.IsAny<Expression<Func<Sdk, object>>[]>()))
.Returns(sdks.AsQueryable);

请使用

mockRepo.Setup(k => k.AllIncluding(It.IsAny<Expression<Func<Sdk, object>>[]>()))
.Returns((Expression<Func<Sdk,
object>>[] includeProperties) => sdks.AsQueryable());

关于c# - 起订量参数 TargetParameterCountException : Parameter count mismatch Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7420235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com