2015-04-23 48 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类的一种方法。这也让我感到困惑,这就是为什么我需要帮助来理解单元测试这个功能的正确方法。 –

相关问题