Skip to content

Instantly share code, notes, and snippets.

@wh1pch81n
Created March 7, 2017 04:08
Show Gist options
  • Save wh1pch81n/986af66fac0e880fb8fcac006e85f707 to your computer and use it in GitHub Desktop.
Save wh1pch81n/986af66fac0e880fb8fcac006e85f707 to your computer and use it in GitHub Desktop.
What if you want to put some common unit test code in a framework? By default, you can not. However if you create protocols for XCTestCase and company, you can.
// Framework code
public protocol _XCTestCaseProtocol: NSObjectProtocol {
func _expectation(description: String) -> _XCTestExpectationProtocol
}
public protocol _XCTestExpectationProtocol {
func fulfill()
}
extension _XCTestCaseProtocol {
public func _expectation(description: String) -> _XCTestExpectationProtocol {
return perform(NSSelectorFromString("expectationWithDescription:")
, with: description).takeUnretainedValue() as! _XCTestExpectationProtocol
}
}
// --------------------------------------------------------
// Unit Test Bundle
extension XCTestCase: _XCTestCaseProtocol {}
extension XCTestExpectation: _XCTestExpectationProtocol {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment