2011-11-30 78 views
0

下面是连贯接口:如何嘲笑连贯接口与犀牛模拟

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

使用在代码为:

串errorReport = ErrorReporter.Add(例外)。准备() ;

模拟测试情况:

 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。

If I comment Expect.Call(errorReporter.Prepare())。Return(string.Empty);那么它对我来说没有任何意义。

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

+0

如果我按以下方式分解我的代码,那么测试运行良好。 ErrorReporter.Add(例外); string errorReport = ErrorReporter.Prepare();我不想破解我的代码 – milind

回答

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

您需要告诉模拟对象将您期望从调用添加的对象返回以将这些调用链接在一起。老实说,我很惊讶,它不会失败,当一个空引用抛出时,当Add返回null并且Prepare在一个空引用上被调用时。

+0

优秀!!!谢谢。 – milind