I'm writing unit test case where the EAAccessory object is required in my testing module.
So I was tried creating EAAccessory object without connecting to real bluetooth device and EAAccessoryManager, But I was unable to assign isConnected and serialNumber to EAAccessory instance since those are readonly variables.
Hence I decided to mocking EAAccessory class and created EAAccessoryMock class by inheriting EAAccessory class and overriding isConnected and serialNumber variables to return my own values.
I thought everything was fine since there was no compiler error. But I received runtime error given below.
caught "EAAccessoryInitException", "-init not supported. EAAccessoryManager is responsible for creating all objects."
So Can anyone please guide me to mock EAAccessory class OR if there is any other way to create EAAccessory object without mocking and without connecting to real bluetooth device?
You unfortunately won't have much luck with creating your own instances of
EAAccessoryobjects or subclassing the class. There is a better way of mocking though that won't require touching theEAAccessoryclass itself (well, almost).The easiest way to do this would be to define a protocol that contains all of the
EAAccessoryvalues that you need, then define an empty extension to theEAAccessoryclass that conforms to the new protocol, like so:Then, if you're listening for accessories using the
EAAccessoryDidConnectnotification, you can unwrap the value in theuserInfodictionary as your new protocol type instead ofEAAccessory:For testing and mocking the accessory, now all you need to do is create a new class or struct that conforms to the
MyAccessoryprotocol instead of theEAAccessoryclass: