Last active
December 3, 2017 20:54
-
-
Save LucianoPAlmeida/137141c27c2382399e08cc59f1b5ff6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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