以下是我的通用基础存储库接口
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模板网!
ASP.NET Core 使用 Azure Active Directory 进行身份验证并ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 进行身
ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授权不起作用)
ASP Core Azure Active Directory 登录使用角色ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登录使用角色)
如何获取守护进程或服务器到 C# ASP.NET Web API 的How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何获取守护进程或服务器到 C# ASP.N
.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 获取 AAD 访问令牌以与 Microsoft Graph 一起使用)
异步调用时 Azure KeyVault Active Directory AcquireTokenAAzure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(异步调用时 Azure KeyVault Active Directory AcquireTokenAsync 超