← Back to Upcase

Suggestions for learning how to wrap external gems?


(Aaron Renner) #1

I have a project where I am working with OAuth and Quickbooks gems and I’m trying to figure out how to tdd my project against these external dependencies. I’ve see where @joshclayton has developed fakes in the tdd workshop and geo coding on rails book, but I’m still struggling with the concept. I usually run into issues getting my code to run the fake implementation, or developing a fake that I can easily control the results of. I also occasionally have issues where my fakes API drifts away from the actual object’s API and I don’t notice it until something breaks.

Any tips or resources for doing TDD with external services would be greatly appreciated. Also, I’ve heard people talk about creating a thin wrapper around external gems to allow easier faking, but I’m still struggling around how to build one effectively.

Thanks in advance for your help. I hope you come out with a book like ruby science for testing in the near future.

Aaron


(Pedro Moreira) #2

hello @aaronrenner, I faced some similar problems myself.

I am really not familiar with Quickbooks or with how you are combining it with oauth, but from what I gather from your question, you are on the right track.

Try creating a wrapper around the gem (this is just a class that you control that is responsible for calling the gem’s methods) and apply the fake along the lines of what @joshclayton does in the TDD workshop. That will give you a single place in your system that interacts with the outside world and so it will make swapping the gem for the fake easier.

I’ll leave you a list of links that helped me implement my solution, hope they help you too :slight_smile:

Tutorial on using Webmock, VCR and Sinatra for testing with external services by thoughtbot’s own @harlow and @adarshpandit :slight_smile: This was really what solved it for me (thank you guys!)

Integrating 3rd-Party APIs and How to test external APIs by Jared Carrol has some good examples of using VCR and wrappers around external services here

Depend upon abstractions by @benorenstein is a good primer of creating a wrapper around a gem in your system.


(Josh Clayton) #3

@aaronrenner @pedromoreira nails it on the head. As for the thin wrapper, in Test-Driven Rails, we create a Searcher class which wraps Twitter; there’s not much else to it! I’d dig through those resources first, and if you have any more questions, let me know!