-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OCMock stubbing with String return type #378
Comments
@TadeasKriz might know, he's the brain behind integrating OCMock into Cuckoo. |
Update on this: Turns out this is really a problem with anything that's a swift value type that the language sort of auto-converts from Objective-C. For example when mocking Seems there are a couple workarounds:
|
I managed to make this work by utilizing By adding this to my project I managed to make it work for import Cuckoo
extension Stubber {
func when<OUT: _ObjectiveCBridgeable>(_ invocation: @autoclosure () -> OUT) -> StubRecorder<OUT._ObjectiveCType> {
when(invocation()._bridgeToObjectiveC())
}
func when<OUT: _ObjectiveCBridgeable>(_ invocation: @autoclosure () -> OUT?) -> StubRecorder<OUT._ObjectiveCType?> {
when(invocation()?._bridgeToObjectiveC())
}
}
extension StubRecorder {
func thenReturn<T: _ObjectiveCBridgeable>(
_ value: T
) where T._ObjectiveCType == OUT, T._ObjectiveCType: NSObject {
thenReturn(value._bridgeToObjectiveC())
}
func thenReturn<T: _ObjectiveCBridgeable>(
_ value: T?
) where T._ObjectiveCType? == OUT, T._ObjectiveCType: NSObject {
thenReturn(value?._bridgeToObjectiveC())
}
} |
The solution works perfectly for bridgeable classes, such as String. But how do we create stubbing with Enumeration type?
Can't be built now. The compiler reports |
So I was trying to mock UserDefaults
string(forKey:)
method using OCMock. My attempt:This blows up spectacularly with a segfault. After digging into a bit I have a couple of theories. It seems to me this is some issue with NSString vs String types. The method signature for UserDefaults (despite being an objective-c type) as exposed to Swift returns a
String?
. It seems probable that it honestly returns anNSString
and there is some language construct that just converts that.Note that other UserDefaults kinds of things work as expected (bool, for example).
Any advice for how to get around this?
The text was updated successfully, but these errors were encountered: