if there is an
if statement, do I need to test for
A Test is meant to exercise your code and show how it works.
- It shows the happy paths where everything works.
- It shows the unhappy paths where something goes wrong and how that is communicated.
If I want to use this function/object/API/software system, I should be able to look at these tests and understand what will happen when I pass X in. Will it return null, return an object, throw an exception, will it block, is it asynchronous?
Now your code has a branch:
if true do X else do Y.
Lets say that you test for branch X, but do not test branch Y. Is Y valid?
If the language has no problem with it, why is it even important that Branch Y is valid?
Flip that on its head, why is it important that Branch X is valid? The answer will probably be something like "Branch X is business behaviour", "Branch X is needed by feature Z", or "Branch X is when everything works". Branch Y represents the contra-case: when the Business Behaviour is different, or the feature does not need X, or something went wrong. In short there is a valid scenario where Branch Y will be picked. Action: Describe that scenario in the test.
That was a simplification, there is one more case: "Branch Y is never used". Action: Delete that branch.
For context consider GoTo Fail. This bug could have easily been identified by a series of tests that exercised each branch. Not doing so opened up that software to misbehaviour.
I would search YouTube for a few videos, and elsewhere for a book:
- Look for any of Kent Beck's videos on Unit Testing, also find one of his unit testing/extreme programming books.
- Look for a presentation called GUTS by Kevlin Henney, also look at his other presentations about errors.
As for syntax refer to the Jasmine Documentation.