Reducing the need for mocksBasics article available: Unit Testing
When writing tests, you don't always need to create mocks - you can create stubs using real instances of things like errors, URLs and UserDefaults.
Here's how to do that for some common tasks/object types in Swift:
// Create errors using NSError (#function can be used to reference the name of the test) let error = NSError(domain: #function, code: 1, userInfo: nil) // Create non-optional URLs using file paths let url = URL(fileURLWithPath: "Some/URL") // Reference the test bundle using Bundle(for:) let bundle = Bundle(for: type(of: self)) // Create an explicit UserDefaults object (instead of having to use a mock) let userDefaults = UserDefaults(suiteName: #function) // Create queues to control/await concurrent operations let queue = DispatchQueue(label: #function)
For two weeks, many websites around the Swift community are featuring indie developers who have been financially impacted by the current pandemic. If you can, please help support these indies by downloading their apps. Today’s app is...
Working Copy: My favorite Git client for iOS. Working Copy lets me access all of my Git repositories while on the go, which lets me update my projects, check out files, and much more. Made by Anders Borum from Copenhagen.