jkulak
jkulak

Reputation: 908

What's the right approach for updating unit tests, when logic has changed?

To make it simple: first, myFunction returned array with two keys ('foo' => 1, 'bar' => 2), and I was testing for existence of each of them.

Later business logic changed, and myFunction should return only one key, array('foo' => 1).

Should I: a) Remove tests for second key ('bar') from unit tests? b) or test directly if this key is not existing in return array of myFunction?

First answer seems more logical - as I'm not testing any other keys like ('blabla', 'xman') and so on. But on the other hand I'd like to be sure that I have my logic updated everywhere.

Upvotes: 0

Views: 67

Answers (2)

marvelTracker
marvelTracker

Reputation: 4969

By definition unit test means testing one functionality in atomic way. You may use Red, Green, refactor approach to fix that problem. This describes TDD approach http://techno-fear-killer.blogspot.com/

Upvotes: 0

Kaken Bok
Kaken Bok

Reputation: 3395

You should test against your current requirement. In your case it includes that the refactoring has been done properly :D

In any case:

If your array should return only foo you test whether it returns only foo or not. If it returns more than foo or something different, the test should fail. If you have a list of keys, you test all these keys whether they are valid or not. There cannot be keys that were not defined in your test set up.

Upvotes: 1

Related Questions