Hacker News new | ask | show | jobs
by Jugurtha 2072 days ago
Well, I had to use mocks when interacting with a device through Bluetooth Low Energy. It's a hardware device that can't be in a CI/CD pipeline.

I pushed the hardware part into a class, injected it as a dependency to client code so I could materialize the BLE connection whenever I wanted.

The binary encoding/decoding wasn't tied to the class and was handled by a codec I wrote to decouple packet handling from the device. You only had to feed it a YAML file I wrote to model the device communication protocole. I read the manufacturer's specs and came up with a schema for rx/tx padding, etc.

When we changed devices, we just had to give another YAML file to the codec and the code worked.

All this to say that it was really hard to test before code refactoring and loosening the dependency on the actual device.

Once we had a nice interface, you could write a MockBLEDevice class even by inheriting from the real class, just changing the actual BLE connection to return something else and keeping everything else the same.