Moq期望IRepository传递表达

Ste*_*orn 9 unit-testing moq

我正在使用此代码来验证我正在测试的方法的行为:

    _repository.Expect(f => f.FindAll(t => t.STATUS_CD == "A"))
    .Returns(new List<JSOFile>())
    .AtMostOnce()
    .Verifiable();
Run Code Online (Sandbox Code Playgroud)

_repository定义为:

private Mock<IRepository<JSOFile>> _repository;
Run Code Online (Sandbox Code Playgroud)

运行我的测试时,我得到以下异常:

表达式t =>(t.STATUS_CD ="A")不受支持.

如果我不能将表达式传递给Expect方法,有人可以告诉我如何测试这种行为吗?

谢谢!!

小智 0

在 Rhino Mocks 中你会做这样的事情......

不要使用 Expect,而是使用 Stub 并忽略参数。然后有——

Func<JSOFile, bool> _myDelegate;

_repository.Stub(f => FindAll(null)).IgnoreArguments()
   .Do( (Func<Func<JSOFile, bool>, IEnumerable<JSOFile>>) (del => { _myDelegate = del; return new List<JSOFile>();});
Run Code Online (Sandbox Code Playgroud)

调用真实代码

*设置一个假的 JSOFile 对象,并将 STATUS_CD 设置为“A”*

Assert.IsTrue(_myDelegate.Invoke(fakeJSO));
Run Code Online (Sandbox Code Playgroud)