如何用Rhino模拟流畅的接口

本文关键字:接口 模拟 何用 Rhino | 更新日期: 2023-09-27 18:16:04

下面是流畅的接口:

public interface IReporter<in T,out TResult>
{
    IReporter<T, TResult> Add(T seed);
    TResult Prepare();
}

string errorReport = ErrorReporter.Add(exception).Prepare();

模拟测试案例:

        With.Mocks(mockRepository)
            .Expecting(() =>
                           {
                               Expect.Call(errorReporter.Add(null)).IgnoreArguments();
                               Expect.Call(errorReporter.Prepare()).Return(string.Empty);
                               Expect.Call(notifier.Notify(null)).IgnoreArguments().Return(true);
                           })
            .Verify(() =>
                        {
                            ITransporter transporter = new Transporter
                            {
                                ExpectedArgsLength = 1,
                                Notifiers = notifiers,
                                ErrorReporter = errorReporter
                            };
                            transporter.Run(new string[] { });
                        });

错误:

Rhino.Mocks.Exceptions。ExpectationViolationException: IReporter ' 2.Prepare();期望#1,实际#0.

如果我注释Expect.Call(errorReporter.Prepare()).Return(string.Empty);然后它工作了,这对我来说没有意义。

我错过了什么吗?请帮助!

如何用Rhino模拟流畅的接口

Expect.Call(errorReporter.Add(null)).IgnoreArguments().Return(errorReporter);

您需要告诉模拟对象从Add调用返回您期望的对象,以便将这些调用链接在一起。老实说,我很惊讶它没有失败与nullreferenceexception时,添加返回null和准备是在一个空引用上调用。