简体   繁体   English

使用Moq:mock对象抛出'TargetParameterCountException'

[英]Using Moq: mock object throwing 'TargetParameterCountException'

I am new to Moq, so hopefully I am just missing something here. 我是Moq的新手,所以希望我在这里错过了一些东西。 For some reason I am getting a TargetParameterCountException. 出于某种原因,我得到一个TargetParameterCountException。

Can you see what I am doing wrong? 你能看出我做错了什么吗? Any questions? 任何问题? Please ask. 请问。 :) :)

Here's my code: 这是我的代码:

[Test]
  public void HasStudentTest_SaveToRepository_Then_HasStudentReturnsTrue()
  {
     var fakeStudents = new List<Student>();
     fakeStudents.Add(new Student("Jim"));

     mockRepository.Setup(r => r.FindAll<Student>(It.IsAny<Predicate<Student>>()))
                                .Returns(fakeStudents.AsQueryable<Student>)
                                .Verifiable();

     // in persistence.HasStudent(), repo.FindAll(predicate) is throwing 
     // 'TargetParameterCountException' ; not sure why
     persistence.HasStudent("Jim");
     mockRepository.VerifyAll();
  }

Here's the HasStudent method from Persistence: 这是Persistence的HasStudent方法:

public bool HasStudent(string name)
  {
     // throwing the TargetParameterCountException
     var query = Repository.FindAll<Student>(s => s.Name == name); 

     if (query.Count() > 1)
        throw new InvalidOperationException("There should not be multiple Students with the same name.");

     return query.Count() == 1;
  }

This is way late to the question but for the sake of Googlers... 这是问题的迟到但是为了Google员工......

I have a very similar case, and I can't explain why, but the problem seems to be with calling AsQueryable on a generic List inside the .Returns(). 我有一个非常类似的情况,我无法解释原因,但问题似乎是在.Returns()内的泛型List上调用AsQueryable。 Problem was solved by setting up the List as IQueryable before the mock setup. 通过在模拟设置之前将List设置为IQueryable来解决问题。 Something like... 就像是...

var fakeList = new List<foo>.AsQueryable();
...
mockRepository.Setup(r => r.FindAll<foo>(It.IsAny<foo>()))
                            .Returns(fakeList)
                            .Verifiable();

What is the signature of the FindAll method? FindAll方法的签名是什么? Does your repository have overloaded FindAll methods? 您的存储库是否重载了FindAll方法?

If so, that may be the explanation. 如果是这样,那可能就是解释。 Your lamda expression can compile into several different types, such as Predicate<Student> , Func<Student, bool> or Expression<Func<Student, bool>> . 你的lamda表达式可以编译成几种不同的类型,例如Predicate<Student>Func<Student, bool>Expression<Func<Student, bool>>

I'm not sure I understand exeactly what is going on, but TargetParameterCountException is a type that belongs to the System.Reflection namespace, so that indicates that Moq somehow tries to invoke a method with the wrong number of arguments. 我不确定我是否正在理解发生了什么,但TargetParameterCountException是一个属于System.Reflection命名空间的类型,因此这表明Moq以某种方式尝试使用错误数量的参数调用方法。 The most common cause for that is when members are overloaded and the wrong overload ends up being invoked... 最常见的原因是当成员超载并且错误的重载最终被调用时...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM