Moq Params TargetParameterCountException : Parameter count mismatch Exception

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

Following is my generic base repository interface

public interface IRepository {     IQueryable AllIncluding(params Expression>[] includeProperties); }

my entity

public class Sdk  {     public Sdk()     {        this.Identifier = Guid.NewGuid().ToString();     }      public virtual ICollection AccessibleResources { get; set; }      public string Identifier { get; set; }     }

and following is the specific repo

public interface ISdkRepository : IRepository { }

now I am trying to test a controller, using moq

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();     var sdks = new HashSet()     {         new Sdk()         {             Identifier = cid,             AccessibleResources = new HashSet()             {                 new Resource()                 {                     Id = Guid.NewGuid(),                     Description = "This is sdk"                 }             }         }     };     mockRepo.Setup(k => k.         AllIncluding(It.IsAny>[]>()))                        .Returns(sdks.AsQueryable);     var sdkCtrl = new SdksController(mockRepo.Object);     var returnedJson=sdkCtrl.GetResources(cid);     returnedJson.ToString(); }

and it is throwing:

System.Reflection.TargetParameterCountException : Parameter count mismatch

Don't know why?

回答1:

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.

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

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.

Update

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



回答2:

Though there is an answer marked as accepted, I believe there is a way to mock your repository correctly.

Instead of

mockRepo.Setup(k => k.AllIncluding(It.IsAny>[]>()))                      .Returns(sdks.AsQueryable);

please use

mockRepo.Setup(k => k.AllIncluding(It.IsAny>[]>()))                      .Returns((Expression>[] includeProperties) => sdks.AsQueryable());


回答3:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!