简体   繁体   English

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

[英]How to mock fluent interface with Rhino mock

Below is fluent interface: 下面是流畅的界面:

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

Using in the code as: 在代码中用作:

string errorReport = ErrorReporter.Add(exception).Prepare(); 字符串errorReport = ErrorReporter.Add(exception).Prepare();

Mock test case: 模拟测试用例:

        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[] { });
                        });

Error: 错误:

Rhino.Mocks.Exceptions.ExpectationViolationException : IReporter`2.Prepare(); Rhino.Mocks.Exceptions.ExpectationViolationException:IReporter`2.Prepare(); Expected #1, Actual #0. 预期#1,实际#0。

If I comment Expect.Call(errorReporter.Prepare()).Return(string.Empty); 如果我评论Expect.Call(errorReporter.Prepare())。Return(string.Empty); then it works which doesn't make sense to me. 然后它对我来说没有意义。

Am I missing something? 我想念什么吗? Please help! 请帮忙!

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

you need to tell the mock object to return the object you expect from the call to Add in order to chain these calls together. 您需要告诉模拟对象从对Add的调用中返回所需的对象,以将这些调用链接在一起。 honestly, i'm suprised it doesn't fail with a nullreferenceexception when Add returns null and Prepare is called on a null reference. 老实说,我很惊讶当Add返回null并且在null引用上调用Prepare时,它不会以nullreferenceexception失败。

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

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