Re: Testing against an api



Brent Collier 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 test 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 development workstation!).

The other rule is Mike Feathers's guideline for testing: "Test cases should never touch the file system, the database, or the wires." That's not a rule, it's just a head-game to encourage his disciples to decouple their code and improve its isolation and encapsulation. Intermediate logic code should not have 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 low-level responses as strings.

Use Mocha to mock the HTTP::Post activity (or whatever), and copy those tests into the .returns(). Then run your tests with your network wire unplugged (yes, and take a long uneasy break from twitter, boingboing, apod, chat, google, RSS, and this newsgroup!), and make sure all your tests still pass.

--
Phlip
.



Relevant Pages

  • Unknown Exception when processing a folder of forms
    ... My organization has a custom Outlook form, designed in-house, for ... submitting vacation and sick leave requests. ... server in a different state. ... Connectivity to this remote server isn't ...
    (microsoft.public.outlook)
  • Re: Testing against an api
    ... tests/specs to actually hit the api, or should the get/post/whatever ... requests be stubbed out? ... I'm currently writing a Ruby gem for a particular micro-blogging api. ...
    (comp.lang.ruby)
  • Re: How can I syncronize the time on my local machine with that of another machine on the network?
    ... > Synchronizing Date and Time to a Remote Server at ... I have a program that needs to set the system time to that of another ... Does anyone out there know of an API perhaps that will do this? ... >: William Hildebrand ...
    (microsoft.public.vb.winapi)
  • Re: how to call an API on a remote machine
    ... I have an application installed on a remote server that also has an SDK ... I want to be able to access some of the API stuff within the SDK ... my local machine? ...
    (microsoft.public.dotnet.languages.vb)
  • server-status and mod_proxy
    ... I have a reverse proxy set up to pass all requests to a remote server: ... the problem with this configuration is I can't check the ...
    (comp.infosystems.www.servers.unix)