11

I am implementing an iOS app written in Swift 2.2 that interacts with a bluetooth device (CBPeripheral). I've got a class, BLEManager, which acts as an interface for interacting with a CBCentralManager instance. It provides methods like:

func startScanning() {    
   cbCentralManagerInstance.scanForPeripheralsWithServices()
}

and

func disconnect() {
   cbCentralManagerInstance.cancelPeripheralConnection()
}

as well as it implements the CBCentralManagerDelegate methods as an extension:

func centralManager(:didDiscoverPeripheral)
func centralManager(:didConnectPeripheral)
func centralManager(:didDisconnectPeripheral)
func centralManagerDidUpdateState(:)

How can I test BLEManager in such a way that it tests that the CBCentralManagerDelegate methods are called and behavior verified?

This question:

How does one unit test code that interacts with the Core Bluetooth APIs?

had been asked and a brief abstract answer was given, but I am at a loss for where or how to even start. The answer mentions verifying code interaction with the API, but what would this look like? How do the delegate methods get fired off if I need a real CBPeripheral instance as a dependency?

I have looked all over, finding examples like:

  1. https://github.com/DeliciousRaspberryPi/MockFive/issues/1
  2. https://swifting.io/blog/2016/06/06/17-unit-test-all-the-things/
  3. http://markspanbroek.github.io/MockingInSwift/

but they give brief overviews and simple code examples to demonstrate a point they are trying to explain, but that doesn't seem to be enough explanation for me to have an idea on how to implement and where to start.

PLEASE HELP!! Code examples are preferred with explanations/answers. :)

Community
  • 1
  • 1
BenS
  • 331
  • 2
  • 12

0 Answers0