2015-04-23 52 views
1

我要測試的FillUpWater功能如下?任何人都可以告訴我,如果我的單元測試這個功能是正確的?

public bool FillUpWater() 
{ 
    WaterTap tap = new WaterTap(); 
    if (tap.FillUpContainer()) 
    { 
     Level = 5; 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
public void FillUpWater() 
{ 
    throw new NotImplementedException(); 
} 

我的單元測試:

[TestClass()] 
public class WaterContainer 
{ 
    [TestMethod()] 
    public void WhenWaterContainerIsEmpty_FillingItUp_CausesCorrectWaterLevel() // Uppgift 4: Vattenbehållaren fylls av 
    {                    // vattenkranen automatisk om den är tom 
     // arrange 
     WaterContainer waterC = new WaterContainer(0); 
     WaterTap tap = new WaterTap(); 

     // act 
     waterC= tap.FillUpContainer(); 
     // assert 
     Assert.AreEqual(5, WaterC.Level); 
     //Assert.IsTrue(tap.FillUpContainer()); 
    } 
} 
+2

是什麼讓你覺得它可能不正確?你有沒有試過編譯和運行它?它產生了意想不到的結果嗎?如果是這樣,你得到了什麼,你期望什麼? (提示:你的兩個代碼示例都不會編譯)。 –

+0

1)確定您想要通過測試來驗證的內容。 2)編寫測試。 3)確保測試實際測試你認爲它做什麼,即確保你看到它失敗,並*由於正確的原因失敗*。 (理想情況下,在你實現/修復一個bug之前編寫測試*如果你之後做了這個測試,在本地修改實現以驗證你的測試。) - 你的問題聽起來像是你錯過了第一步... – nodots

+0

Hi Fredrik !我嘗試運行測試但遇到了som錯誤,即WaterContainer()不包含一個構造函數,它只帶1個參數。 WaterTap()由於保護級別而不可訪問,但是當我檢查時,它既不受保護也不私密。上面我想測試的函數應該在容器降到-1時將容器填充到5。我應該如何着手正確編寫這個測試?我假設龍頭對象龍頭填充waterContainer對象waterC。 –

回答

1

我可以在這裏看到了一些問題。我已將每個問題置於評論中...

[TestClass()] 
public class WaterContainer 
{ 
    [TestMethod()] 
    public void WhenWaterContainerIsEmpty_FillingItUp_CausesCorrectWaterLevel() { 

     // There seems to be no relationship between the container 
     // and the tap - so how will the tap cause any change 
     // to the container? 
     WaterContainer waterC = new WaterContainer(0); 
     WaterTap tap = new WaterTap(); 

     // The method that you shared with us is called: 
     // FillUpWater, but this is calling FillUpContainer 
     waterC= tap.FillUpContainer(); 

     // You create a variable named: 
     // waterC, but use WaterC here (C# is case sensitive) 
     Assert.AreEqual(5, WaterC.Level); 
    } 
} 
+0

嗨,史蒂夫!你可以看到我在單元測試中還是個新手。我的意思是上面的代碼是我假設WaterTap類的tap對象會在水平降到-1時立即填充waterContainer對象waterC。 FillUpContainer是WaterTap類的bool方法,返回true或false。 FillUpWater是WaterContaner類的一種方法。這也讓我感到困惑,這就是爲什麼我需要幫助來理解單元測試這個功能的正確方法。 –

相關問題