2011-01-21 72 views
0

我目前正在學習Rhino-mocks,並認爲我混淆了單元測試和嘲諷之間的界限。在我下面的例子中,我有一個只讀的Count()屬性,我試圖對Get()進行測試(僅供討論使用的一個非常人爲的例子)。正如Assert.AreEqual的註釋所示,Count()屬性的結果應爲3時的結果爲0.單元測試getter/setter時,如何使用Rhino-mocks?

我的問題是,我可以使用Rhino-mocks實際存根對象(在本例中爲只讀屬性)並測試模擬IProduct對象的get_Count()屬性的邏輯

public interface IProduct 
    { 
     int Count { get; } 
    } 

    public class Product : IProduct 
    { 
     private int count; 
     public int Count 
     { 
      get { return count + 1; } 
     } 
    } 

    public class TestFixture 
    { 
     [NUnit.Framework.Test] 
     public void TestProduct() 
     { 
      MockRepository mock = new MockRepository(); 
      IProduct product = mock.Stub<IProduct>(); 

      product.Stub(p => p.Count).Return(2); 
      mock.ReplayAll(); 

      Assert.AreEqual(3, product.Count); //Fails - result from product.Count is 2 
      mock.VerifyAll(); 
     } 
    } 
+2

如果你剛剛開始嘲笑,不要學習記錄/重播methodolgy - 閱讀犀牛支持的AAA(arrange-act-assert)語法。 – 2011-01-21 16:54:46

回答

2

您正在嘲笑您嘗試測試的對象。這從根本上是不正確的 - 你想對你試圖測試的對象進行模擬(或存根)依賴。

在上面所示的情況下,您完全不會使用模擬。

另請參閱我對AAA語法的評論。

+0

我認爲你的意思是你想模擬依賴關係,而不是依賴關係,也就是說,你想模擬依賴於測試對象的對象。 – 2012-12-19 17:57:28

相關問題