Brent C. wrote:
When testing code that consumes a web service, is it bad for the
tests/specs to actually hit the api, or should the get/post/whatever
requests be stubbed out?
Two reasons it’s bad. The first is a simple rule “never hit the wire at
time”. It annoys the remote server, and exposes your test runs to false
negatives if that server is down (or when it finally blacklists your
The other rule is Mike Feathers’s guideline for testing: “Test cases
never touch the file system, the database, or the wires.” That’s not a
it’s just a head-game to encourage his disciples to decouple their code
improve its isolation and encapsulation. Intermediate logic code should
runaway dependencies on low-level code with side-effects.
I’m currently writing a Ruby gem for a particular micro-blogging api. My
tests actually make requests against the api and I was just wondering if
maybe I shouldn’t be doing that…
Run those tests one last time, with p statements that barf out the
responses as strings.
Use Mocha to mock the HTTP::Post activity (or whatever), and copy those
into the .returns(). Then run your tests with your network wire
and take a long uneasy break from twitter, boingboing, apod, chat,
and this newsgroup!), and make sure all your tests still pass.