基于状态的测试
给定这个简单的类,我们可以通过断言在调用 ShaveHead
方法后将 HairLength
变量的状态设置为零来测试 ShaveHead
方法是否正常工作。
public class Person
{
public string Name;
public int HairLength;
public Person(string name, int hairLength)
{
this.Name = name;
this.HairLength = hairLength;
}
public void ShaveHead()
{
this.HairLength = 0;
}
}
[Test]
public void Person_ShaveHead_SetsHairLengthToZero()
{
Person personUnderTest = new Person("Danny", 10);
personUnderTest.ShaveHead();
int hairLength = personUnderTest.HairLength;
Assert.AreEqual(0, hairLength);
}