2011-11-30 96 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