2013-02-03 25 views
2

當我嘗試運行我的測試時,出現以下錯誤。我試過IgnoreArguments(),但這似乎沒有辦法。當我嘗試mock.Ordered()如果我做mock.Record()然後它通過,這個異常只會被拋出。爲什麼我得到這個錯誤:需要返回值或拋出的異常?

System.InvalidOperationException:以前的方法 'IProductRepository.GetAllProducts();'需要返回值或拋出的異常。


[Test] 
     public void TestwithOrderedRecordsandPlayBack() 
     { 
      var mock = new MockRepository(); 
      var productRepository = mock.StrictMock<IProductRepository>(); 

      var product = new Grains 
      { 
       Name = "Wonder Bread", 
       Category = "Grains" 
      }; 

      IList list = new ArrayList(); 
      //Class under test 
      var service = new ProductService(productRepository); 

      using (mock.Ordered()) 
      { 
       Expect.Call(productRepository 
        .GetAllProducts()) 
        .IgnoreArguments() 
        .Return(new ArrayList()); 
       Expect.Call(()=>productRepository.SaveProduct(product)); 
      } 

      using (mock.Playback()) 
      { 
       list = service.GetAllProducts(); 
       service.SaveProduct(product); 
      } 
     } 

服務電話:

public virtual IList GetAllProducts() 
     { 
      IList list = _productRepository.GetAllProducts(); 
      return list; 
      //throw new System.Exception("Not implemented"); 
     } 



public virtual IList GetAllProducts() 
     { 
      IList list = _productRepository.GetAllProducts(); 
      return list; 
      //throw new System.Exception("Not implemented"); 
     } 
+0

那麼你的服務電話是什麼樣的?它真的是一個如此薄的層,它只是將請求逐字傳遞給你的存儲庫? –

+1

這不是關於你*能夠擁有的東西 - 而是你的代碼具有什麼功能? –

+0

@JonSkeet,我把它放在上面的編輯中。 – Fabii

回答

2
 using (mock.Ordered()) 
     { 
      Expect.Call(productRepository 
       .GetAllProducts()) 
       .IgnoreArguments() 
       .Return(new ArrayList()); 
      Expect.Call(() => productRepository.SaveProduct(product)); 
     } 

這個失敗,你說的完全一樣的錯誤。 System.InvalidOperationException:上一個方法'IProductRepository.GetAllProducts();'需要返回值或拋出異常。

原因是模擬存儲庫永遠不會離開記錄模式。您可能會認爲(我也是)mock.Ordered()將存儲庫置於某種有序記錄狀態,但它不。因此,您需要指定完成錄製行爲的時間。

您可以通過兩種方式來實現:

 using (mock.Record()) 
     using (mock.Ordered()) 
     { 
      Expect.Call(productRepository.GetAllProducts()).Return(new ArrayList()); 
      Expect.Call(() => productRepository.SaveProduct(product)); 
     } 

或掉落mock.ReplayAll()您完成錄製時:

 using (mock.Ordered()) 
     { 
      Expect.Call(productRepository.GetAllProducts()).Return(new ArrayList()); 
      Expect.Call(() => productRepository.SaveProduct(product)); 
     } 

     mock.ReplayAll(); 

     using (mock.Playback()) 
     { 
      service.GetAllProducts(); 
      service.SaveProduct(product); 
     } 

我測試了,都工作。

+0

謝謝,讓我知道你什麼時候得到更多的細節。 – Fabii

+1

我明白了,更新了答案。 – bas

+0

優秀,謝謝。 – Fabii

0

不是100%肯定,但放棄了它的答案,讓您可以輕鬆地閱讀答案/評論。 讓我知道如果這不是解決方案,那麼我會刪除答案,以避免混淆。

我覺得你的問題是在

Expect.Call(()=>productRepository.SaveProduct(product))

您在記錄模式已經(mock.Ordered()),所以沒有必要指定Expect.Call()

Expect.Call()要求返回值。您只是在尋找一種方法來驗證方法保存被調用。

我想你應該改變你的代碼:

 using (mock.Ordered()) 
     { 
      Expect.Call(productRepository 
       .GetAllProducts()) 
       .IgnoreArguments() 
       .Return(new ArrayList()); 
      productRepository.SaveProduct(product); 
     } 

(你可能會認爲你叫SaveProduct在您的測試方法,但你不會,RhinoMocks是更聰明,並記錄了這一空白通話)

+0

我試過你的建議,我仍然遇到同樣的錯誤。儘管留下這個答案,因爲它提供有關工作或Ordered和Expect的信息。 – Fabii

+0

k hang on然後我會嘗試重現你的錯誤。需要點燃VS. – bas