finnkauski / lighthouse

Philips Hue API wrapper crate for Rust
GNU Affero General Public License v3.0
43 stars 4 forks source link

Tests for more core functionality - mocking request calls to bridge #19

Open finnkauski opened 4 years ago

finnkauski commented 4 years ago

Is your feature request related to a problem? Please describe. This basically relates to the fact that the bridge as of now is not fully tested because we need to mock request calls to a bridge with our own bridge somehow in order to be able to test.

Describe the solution you'd like This could be implemented in the form of a bridge tester struct that spins up its own thread and listens to requests on a given port and responds with premade json files.

ssnover commented 4 years ago

Possible alternative would be to use the a simulator project for the Hue Bridge that already exists (that's more or less what you're suggesting with the premade JSON). The main problem is finding one that's up-to-date with the API: this fork of one I've used was last updated 2 years ago.

Alternatively, (this is how I'd do it in C++, but I don't know if Rust has some details that make this different) you could wrap reqwest::Client in an wrapper that implements a trait. Then you could dependency inject a mock during construction of the Bridge. Either way is kind of a pain admittedly, it'd be great if Philips published their own simulators...

finnkauski commented 4 years ago

The later suggestion about a trait sounds like a good way to mock stuff. I just wonder what burden it adds to the maintenance process. I think easiest would be to write a simple web server (simulator) that is spun up in a seperate thread during testing as a fixture that is able to handle the requests sent by responding with premade json. I think writing it for the few use cases we have isn't that much work using something like actix or whatever else. And in the case of expanding the API coverage more things could get added to it to the point where eventually it could become a fully fledged simulator as you say. But for the mean time a simple web server will probably do.

finnkauski commented 4 years ago

Note to future-self: Rust has a way of defining a custom test runner as well. So I think we could definitely figure something out with a bit of head scratching.