My codebase is using device motion data for perticular functionality. I want to unit test it. For that I need to mock CMMotionManager so that data can be manipulated and test cases can be executed according to requirements. Is there any way out to achieve this?
Callback for following function needs to be mocked
startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)
There are two straightforward ways you could go about mocking this;
You can subclass
CMMotionManagerand overload the function that you want to mock. This will allow you to inject the mock anywhere that you currently useCMMotionManager.When using protocols you will have to use the protocol throughout your codebase in order to be able to inject the mock when needed. This is a layer of abstraction that could be confusing if it is only used for testing purposes.