作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Moq 的新手,所以希望我只是遗漏了一些东西。出于某种原因,我得到了 TargetParameterCountException。
你能看出我做错了什么吗?任何问题?请问。 :)
这是我的代码:
[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();
}
这是 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;
}
最佳答案
这个问题已经晚了,但为了谷歌人......
我有一个非常相似的案例,我无法解释原因,但问题似乎在于在 .Returns() 内的通用列表上调用 AsQueryable。通过在模拟设置之前将列表设置为 IQueryable 解决了问题。像...
var fakeList = new List<foo>.AsQueryable();
...
mockRepository.Setup(r => r.FindAll<foo>(It.IsAny<foo>()))
.Returns(fakeList)
.Verifiable();
关于c# - 使用最小起订量 : mock object throwing 'TargetParameterCountException' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1625945/
我是一名优秀的程序员,十分优秀!