Ale*_*sky 3 c# unit-testing moq mocking
我是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();
}
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
这是问题的迟到但是为了Google员工......
我有一个非常类似的情况,我无法解释原因,但问题似乎是在.Returns()内的泛型List上调用AsQueryable.通过在模拟设置之前将List设置为IQueryable来解决问题.就像是...
var fakeList = new List<foo>.AsQueryable();
...
mockRepository.Setup(r => r.FindAll<foo>(It.IsAny<foo>()))
.Returns(fakeList)
.Verifiable();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1805 次 |
最近记录: |