-
Notifications
You must be signed in to change notification settings - Fork 179
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
Mocking a delegate property with objcStub #358
Comments
I'm afraid you're not using the correct methods for ObjC mocking. Please post the whole test, so I can verify how you create mocks, stub them, and verify them. |
Thanks for the response. Here's a brief test class that illustrates my problem: class CuckooBluetoothDemo: XCTestCase {
var mockCentral: CBCentralManager!
override func setUpWithError() throws {
mockCentral = objcStub(for: CBCentralManager.self) { stubber, mock in
// Neither of these stubs compile:
stubber.when(mock.delegate.set).then { args in // Value of type 'CBCentralManagerDelegate?' has no member 'set'
}
stubber.when(mock.setDelegate).then { args in // Value of type 'CBCentralManager' has no member 'setDelegate'
}
}
}
func testSettingDelegate() throws {
// This line triggers an error because the delegate is not stubbed:
// caught "NSInternalInconsistencyException", "OCMockObject(CBCentralManager): unexpected method invoked: setDelegate:-[CuckooBluetoothDemo testSettingDelegate] "
mockCentral.delegate = self
}
}
extension CuckooBluetoothDemo: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
// This method is just here to satisfy the compiler
}
} |
Hey, I just played around a bit with this. We were missing a Please try branch Thank you so much for bringing this to light, if you come up with any other shortcomings, let us know and we'll hopefully find a way to fix them. Oh and in ObjC mocking, you needn't specify anything like |
Thanks for looking in to this! I've tried out your bug fix branch, which makes it possible to mock out the I've still got an issue though when the class CuckooBluetoothDemo: XCTestCase {
var mockCentral: CBCentralManager!
override func setUpWithError() throws {
mockCentral = objcStub(for: CBCentralManager.self) { stubber, mock in
stubber.when(mock.delegate).thenReturn(self) // this now works with the updated branch!
}
}
func testSettingDelegate() throws {
// This line still fails the test because setDelegate: is not stubbed, with error:
// caught "NSInternalInconsistencyException", "OCMockObject(CBCentralManager): unexpected method invoked: setDelegate:-[CuckooBluetoothDemo testSettingDelegate] "
mockCentral.delegate = self
}
}
extension CuckooBluetoothDemo: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
// This method is just here to satisfy the compiler
}
} |
Thanks for the feedback, I'll take a look at it, hopefully it's nothing too major. |
Hi! I just ran into this myself too, how do I need to change Podfile to add fix/objc-nsobjectprotocol branch? |
Like this?
|
I've changed the Podfile, still getting the same error but I am not sure it is related with the poster´s issue
|
Hey, @quetool. Your issue is not quite relevant to @s-hocking's. Your As for calling the setter for resolving @s-hocking, I'm pretty stumped without the necessary experience in ObjC to include this functionality at the moment. I'll leave the issue open and see if either I or @TadeasKriz gets some bright idea. |
I am having almost the same issue as @s-hocking. Using Looking through the OCMock code and documentation, it seems like maybe the mock that OCMock's method for making "nice" mocks: https://github.com/erikdoe/ocmock/blob/6358799e04cb93d8f126f1ea6a67e2e351b169f1/Examples/iOS5Example/usr/include/OCMock/OCMockObject.h#L22 |
Hey @jandrewmoore, that might work, and even if it doesn't for some reason, it's still a good feature to have. Thanks for doing the research. 🙂 |
Hi there. I'm currently trying to mock out
CBCentralManager
using the experimental ObjC stubbing feature. I've run into an issue with thedelegate
property on this class... If I don't stub thedelegate
property, OCMock fails my test when my code accesses thedelegate
property, with "unexpected method invoked":caught "NSInternalInconsistencyException", "OCMockObject(CBCentralManager): unexpected method invoked: setDelegate:<MySDKProject.BLECentral: 0x7fa1964178a0>
So I then try to stub the
delegate
property, but nothing seems to work.stubber.when(mock.delegate.set).then...
complains with "Value of type 'CBCentralManagerDelegate?' has no member 'set'"stubber.when(mock.setDelegate(any()))
complains thatValue of type 'CBCentralManager' has no member 'setDelegate'
What am I doing wrong?
Edit: for anyone else trying to mock CoreBluetooth classes, this project from Nordic Semiconductor looks pretty good!
The text was updated successfully, but these errors were encountered: