Skip to content

Instantly share code, notes, and snippets.

@LucianoPAlmeida
Last active December 3, 2017 20:54
Show Gist options
  • Save LucianoPAlmeida/137141c27c2382399e08cc59f1b5ff6b to your computer and use it in GitHub Desktop.
Save LucianoPAlmeida/137141c27c2382399e08cc59f1b5ff6b to your computer and use it in GitHub Desktop.
//Example of a runtime check stubbing
class User {
}
enum ResultType<T> {
case success(T)
case error(Error)
}
class DAO {
enum Source {
case server
case stubbed
}
var source: Source
init(source: Source = Source.server ) {
self.source = source
}
}
class UserDAO: DAO {
func getAll(completion: @escaping (ResultType<[User]>) -> Void) {
if source == .server {
print("Real data from the server")
} else {
print("Stubbed data from the mocks")
}
}
}
//In the tests
let daoTest: UserDAO = UserDAO(source: DAO.Source.stubbed)
print("Test")
daoTest.getAll(completion: { _ in })
//On the app
let dao: UserDAO = UserDAO()
print("APP")
dao.getAll(completion: { _ in })
//Output
//Test
//Stubbed data from the mocks
//APP
//Real data from the server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment