Tips to write testable code
One of the biggest challenges developers face when they try to write tests for code is struggling to write tests for existing functions that could be more testable – for example, functions that contain code that performs network requests or functions that have external dependencies that are difficult to set up.
Writing testable code usually goes hand in hand with writing clean and efficient code. However, we should still follow some writing guidelines if we want our functions to be testable.
Let’s explore some of them now.
Writing pure functions
Pure functions are functions that, given the same input, always return the same output and don’t rely on external states or have any side effects.
For instance, take the following example:
class NumberFilter { var numbers: [Int] = [] var filteredNumbers: [Int] = [] func filterNumbers(predicate: (Int) ->...