I'm playing around with TDD and unit testing in general. All the examples I've seen return values and that seems like the easiest case. However what if my function doesn't return a value?
For example let's say I have an Actor class and I need a way to increase it's "health". I made a unit test like below and then make the Actor class to satisfy it, but is this ok and common to do? I don't see many examples using properties in the unit test. Should I be thinking differently with this kind of stuff?
[TestMethod]
public void IncreaseHealth_PositiveValue_PositiveHealth()
{
Actor a = new Actor();
int beforeHealth = a.Health;
a.IncreaseHealth(5);
int afterHealth = a.Health;
Assert.AreEqual(beforeHealth + 5, afterHealth);
}
This test is a good start. However, just like when testing value-returning methods, you should test border conditions on methods with side effects. In this case, you should also check that
Although your use of a locally initialized object is fine, you could also put one on the unit test object, and initialize it in your [Setup]
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With