【发布时间】:2009-10-26 16:58:46
【问题描述】:
我是 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;
}
【问题讨论】:
-
如果将 It.IsAny
替换为 It.IsAny -
@Kirschstein:那不会编译
标签: c# unit-testing mocking moq