# Function Injection for Testable Swift Code

DevFeed: [Function Injection for Testable Swift Code](<https://devfeed.tech/articles/functions-deserve-injection-too-22315.md>)

Original publisher: [Read original article](<https://www.thecodedself.com/functions-deserve-injection/>)

Author: Keegan Rush

Published: 2017-11-28T00:00:00Z

Content type: tutorial

Language: en

Sources: [The Coded Self](<https://devfeed.tech/sources/the-coded-self.md>)

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [swift](<https://devfeed.tech/tags/swift.md>), [testing](<https://devfeed.tech/tags/testing.md>), [tests](<https://devfeed.tech/tags/tests.md>)

## AI overview

This tutorial presents function injection in Swift as a way to pass function dependencies into other functions. It explains how the technique can keep tests short and focused while avoiding repeated validation tests and unnecessary classes or dependency containers.

## Source excerpt

Lately, I've been taking advantage of Swift's functional abilities where it makes sense to help me write concise and clear code that's easy to test. I'd like to share one technique that has helped me to eliminate repetition and breakages of encapsulation in tests: function injection. In traditional dependency injection, an object that is dependended on is passed to the method that depends on it. Function injection is my name for following the same approach when your function consumes another function. As a code base evolves over time, a lot of small, focused utilities are added. Here's one small utility function that I use quite often: extension String { func isNonEmpty() -> Bool { return !trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } } This eliminates the verbosity of checking if a string is empty or contains only whitespace. It is, understandably, quite easy to test as well. func testNoCharactersReturnsTrue() { XCTAssertFalse("".isNonEmpty()) } func testOnlyBlankSpacesReturnsTrue() { XCTAssertFalse(" ".isNonEmpty()) } func testNewlineReturnsTrue() { XCTAssertFalse(" \n ".isNonEmpty()) } func testNonEmptyStringReturnsFalse() { XCTAssertTrue("Hello there".isNonEmpty()) } As an example, imagine that we're using this in an app for a clothings store. When you walk into the store, you should get a push notification from the app with a greetings message containing the user's name. If there's something wrong with the user's name - if it's empty or whitespace - we'll throw an error. struct Greeter { func greet(name: String) throws -> String { guard name.isNonEmpty() else { throw GreetingError.invalidName } return "Welcome, \(name)! Have you seen the specials on offer?" } } Let's write some tests for this function. We want to test two things: The returned message is correct when the name is valid An error is thrown when the business logic determines that a name is invalid func testGreetingWithValidNameReturnsGreetingString() { let expected = "Welcome, Caroline!