2008-09-19 82 views
0

我想實現一些重試邏輯,如果我的代碼中有異常。我編寫了代碼,現在我試圖讓Rhino Mocks模擬這種情況。代碼的JIST如下:犀牛嘲笑有序回覆,拋出異常問題

class Program 
    { 
     static void Main(string[] args) 
     { 
      MockRepository repo = new MockRepository(); 
      IA provider = repo.CreateMock<IA>(); 

      using (repo.Record()) 
      { 
       SetupResult.For(provider.Execute(23)) 
          .IgnoreArguments() 
          .Throw(new ApplicationException("Dummy exception")); 

       SetupResult.For(provider.Execute(23)) 
          .IgnoreArguments() 
          .Return("result"); 
      } 

      repo.ReplayAll(); 

      B retryLogic = new B { Provider = provider }; 
      retryLogic.RetryTestFunction(); 
      repo.VerifyAll(); 
     } 
    } 

    public interface IA 
    { 
     string Execute(int val); 
    } 

    public class B 
    { 
     public IA Provider { get; set; } 

     public void RetryTestFunction() 
     { 
      string result = null; 
      //simplified retry logic 
      try 
      { 
       result = Provider.Execute(23); 
      } 
      catch (Exception e) 
      { 
       result = Provider.Execute(23); 
      } 
     } 
    } 

有什麼事發生的是,異常被拋出每次而不是隻一次。我應該怎樣改變設置?

回答