Add Tests for Existing Code

Our incomplete test results in a warning:

 warning: initialization of immutable value 'result' was never used;
 consider replacing with assignment to '_' or removing it

This is a good clue that we should write an assertion for the result value. Let’s bring this code under test.

We can write tests for existing code in two different ways. They depend on whether the code has proven itself with real use:

  • If the code hasn’t yet shipped, we should write tests that express the requirements for that section of code. This means we need to reason about how the code is supposed to behave. The tests may even catch some bugs.

  • If the code has shipped, then real users are exercising it. As long as no one has found any bugs, ...

Get iOS Unit Testing by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.