I’ve been pondering this lately, also in relation to the talk about more explicit types:
Let’s say we’re making a small tool that needs to talk to GitHubs API. And let’s pretend someone has made an awesome Github shard. It has everything, pass it and token and you can request repos, issues, deployment, the works, and it’ll hide all the nitty gritty HTTP requests for you. Nicely encapsulated, just how we like it.
So we’re making this small tool that, let’s say, asks Github who you are and what repos you have in order to clone them all. So it just needs information from two endpoints in the API (something like
<org>/repos). Which translates to just to method calls on the Github client instance provided by the previously mentioned shard.
Obviously we’d like to test your own code, so we inject the Github client into our own worker class so we can test our class without relying on the shard and in the end a working internet connection to Github.
But… Even when injecting the class, we’re typing it to
Github::Client, as the compiler will insist on this when we put the client in an instance variable. So we’re back to square one.
How does people generally deal with this in practice? Make a module with the methods we really need and add it to the
Github::Client class, so we can have our own mock that just needs to implement that interface? Or are there better tricks I haven’t discovered yet?