# Testing against an api

**URL:** <https://www.ruby-forum.com/t/testing-against-an-api/158463>\
**Category:** Ruby\
**Created:** [January 23, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/testing-against-an-api/158463 "2009-01-23T16:40:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![brentmc79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@brentmc79](https://www.ruby-forum.com/u/brentmc79)\
**Post date:** [January 23, 2009, 4:40pm UTC](https://www.ruby-forum.com/t/testing-against-an-api/158463/1 "2009-01-23T16:40:37Z")

</div>

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?

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…

---

<div class="post-metadata">

**Author:** ![brentmc79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@brentmc79](https://www.ruby-forum.com/u/brentmc79)\
**Post date:** [January 23, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/testing-against-an-api/158463/2 "2009-01-23T16:58:29Z")

</div>

On Fri, Jan 23, 2009 at 5:38 PM, Brent C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Two reasons you shouldn’t

1. It slows down your tests which means your run them less often or stop  
testing eventually
2. Your calls will be directly affecting the remote service and actually  
creating, modifying or deleting things in the process (not usually what  
you  
want to do while testing)

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![brentmc79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@brentmc79](https://www.ruby-forum.com/u/brentmc79)\
**Post date:** [January 23, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/testing-against-an-api/158463/3 "2009-01-23T20:16:17Z")

</div>

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  
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.

---

<div class="post-metadata">

**Author:** ![brentmc79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e47774/32.png) [@brentmc79](https://www.ruby-forum.com/u/brentmc79)\
**Post date:** [January 24, 2009, 2:02am UTC](https://www.ruby-forum.com/t/testing-against-an-api/158463/4 "2009-01-24T02:02:19Z")

</div>

Check out fakeweb which can be used to help mock out your web service  
calls  
easily:  
[http://technicalpickles.com/posts/stop-net-http-dead-in-its-tracks-with-fakeweb](http://technicalpickles.com/posts/stop-net-http-dead-in-its-tracks-with-fakeweb)

Matt

On Fri, Jan 23, 2009 at 2:13 PM, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> development workstation!).
> 
> > 
> 
> –  
> Phlip

–  
Matt T.  
Highgroove Studios  
[www.highgroove.com](http://www.highgroove.com)  
cell: 404-314-2612  
blog: [maraby.org](http://maraby.org)

Scout - Web Monitoring and Reporting Software

> **[Application Monitoring for Ruby, PHP, Node.js, Python, and Elixir ~ Scout](https://scoutapm.com/)**
>
> Rails Monitoring, Django Monitoring, Elixir Phoenix Monitoring, PHP Laravel Monitoring, and more that identifies slow database queries, memory leaks, and slow custom code.
