繁体   English   中英

如何使用Rhino模拟来模拟流畅的界面

[英]How to mock fluent interface with Rhino mock

下面是流畅的界面:

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

在代码中用作:

字符串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); 然后它对我来说没有意义。

我想念什么吗? 请帮忙!

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

您需要告诉模拟对象从对Add的调用中返回所需的对象,以将这些调用链接在一起。 老实说,我很惊讶当Add返回null并且在null引用上调用Prepare时,它不会以nullreferenceexception失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM