c# - Could some one check for me if my unit test for the FillUpWater is correct -
could tell me correct unit test fillupcontainer()?
namespace coffeemaker { [testclass()] public class watercontainer { //private watercontainer waterc; watercontainer waterc = new watercontainer(0); int level = waterc.level; [testmethod()] public void fillupwatertest() { if (waterc.level == 0) { bool result; result = waterc.fillupwater(); } level.equals(5); } } }
no, it's not. level.equals(5)
not assertion, it's expression, result of throw away not assigning anything.
a better test might this:
[testclass()] public class watercontainer { [testmethod()] public void whenwatercontainerisempty_fillingitup_causescorrectwaterlevel() { // arrange watercontainer water = new watercontainer(0); // act water.fillupwater(); // assert assert.areequal(5, water.level); } }
notice method name describes what's being tested, , test self-contained (all local variables).
the assert
statement cause test fail if water level not correct.