I want to test if the water container is refilled when empty, below is my method's code:
//Tests if the water is refilled if empty.
public void RefillWaterContainerTest()
{
Console.Write("Test the water refill");
int EContainer = new WaterContainer();
FillUpContainer();
Assert.AreEqual(0, EContainer);
}
The rest of the code is as follows:
public class WaterContainer : Container {
public WaterContainer(int level) { Level = level; }
public override void GetContent() {
if (HasContent()) {
Level -= 1;
} else {
if (FillUpWater() == false) {
throw new EmptyContainerException("No water.");
} else {
Level -= 1;
}
}
}
protected bool FillUpWater() {
WaterTap tap = new WaterTap();
if (tap.FillUpContainer()) {
Level = 5; return true;
} else {
return false;
}
}
} //And the water tap class: class WaterTap { public bool FillUpContainer() { return true; } }
答案 0 :(得分:0)
If WaterTap.FillUpContainer always returns true, then FillUpWater will never return false, which means that you never throw the EmptyContainerException, making any Tests rather pointless. I suggest fixing that first.
After that you can check if the exception is thrown by using Assert.Throws.
Assert.Throws(EmptyContainerException,FillUpContainer);