In the FizzBuzz code challenge, we had to write the following test method:
1 2 3 4 5 6 7 |
func test_fizzBuzz_whenInputIsMultipleOf3_returnsFizz() { let fizzBuzzCalculator = FizzBuzzCalculator() let result = fizzBuzzCalculator.fizzBuzz(number: 2 * 3) XCTAssertEqual("Fizz", result) } |
Although the test is easy to read and understand, we can do this a bit better. We used as a multiple of 3 the number 2 * 3. To increase the test’s clarity we can write the number 2 as anyIntAboveOne.
1 2 3 4 5 6 7 8 9 10 11 |
private func anyIntAboveOne() -> Int { return 2 } func test_fizzBuzz_whenInputIsMultipleOf3_returnsFizz() { let fizzBuzzCalculator = FizzBuzzCalculator() let result = fizzBuzzCalculator.fizzBuzz(number: anyIntAboveOne() * 3) XCTAssertEqual("Fizz", result) } |
In this way, we communicate our intention more clearly, as we are not interested in the number 2 * 3 but any multiple of 3. This makes sense when we need to set up more complex objects.