Last active
August 5, 2019 14:21
-
-
Save uliwitness/e23a8b69657225cc8dcb89e0158b88d6 to your computer and use it in GitHub Desktop.
You can make an existing type conform to a protocol you define later in Swift.
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
class OriginalClass { | |
var fooProp: Int = 15 | |
func sayHello(_ string: String) { | |
print("Hello \(string) \(fooProp)") | |
} | |
} | |
protocol MockProtocol { | |
var fooProp: Int { get } | |
func sayHello(_ string: String) | |
} | |
extension OriginalClass: MockProtocol {} | |
class MockClass: MockProtocol { | |
var fooProp: Int = 42 | |
func sayHello(_ string: String) { | |
print("MOCKED Hello \(string) \(fooProp)") | |
} | |
} | |
let original: MockProtocol = OriginalClass() | |
let mocked: MockProtocol = MockClass() | |
original.sayHello("Roger") | |
mocked.sayHello("Gracie") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment