I am creating an iOS application with Swift that uses the Apple CoreBluetooth framework to communicate with peripherals via Bluetooth Low Energy. In an attempt to unit test a custom controller that implements the CBCentralManagerDelegate and CBPeripheralDelegate protocols, I provide a test double subclass from CBCentralManager to mimic the behavior of the CoreBluetooth API. Controller delegate callbacks are called at the appropriate time.
So far it works very well. But when it comes to calling CBPeripheralDelegate callbacks, you need to pass CBPeripheral. Normally, I would just fake a component that provides my own subclass, or make fun of an instance of CBPeripheral.
Here's the catch : the designated CBPeripheral initializer in the Objective-C base library is marked as unavailable, which prevents me from creating an instance of CBPeripheral in my fake CBCentralManager and passing it as the CBPeripheralDelegate argument under tests. (Xcode leaves me with a compiler error.)
So far, I have tried to provide a custom initializer in my fake subclass (which does not work because I have to call the impossible init at the end), overriding the initializer with the extension (which apparently is not allowed), and invokes the selector (init ) with runtime Objective-C, as if I don't give a shit that it is unavailable. The latter approach seems to be the most promising, but in fact it does not work and leaves me with unmanaged objects of the wrong type (my test class).
I definitely got to the checkpoint here and would be very happy for any input on
source share