# Testing an API I'm creating, need test server to be running

**URL:** <https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749>\
**Category:** RSpec\
**Created:** [January 14, 2012, 3:43pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749 "2012-01-14T15:43:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 14, 2012, 3:43pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/1 "2012-01-14T15:43:49Z")

</div>

Hi,

As I am writing a brand new API, I’d like to test it’s response.

Obviously, controller tests are great and work like a charm but as I  
expose data which are not really resources, I’ve to make some DIY to  
provide responses that ActiveResource can handle properly.

Basically, it would be great in my case to test if ActiveResource gets  
the expected data but it means I have to launch a test server in  
background for the whole suite.

Is there a convenient to handle this? Currently, I launch the test  
server in console but it would be much better to have this handled  
programmatically.

Thanks in advance,

Ben

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [January 14, 2012, 5:35pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/2 "2012-01-14T17:35:54Z")

</div>

On Jan 14, 2012, at 8:40 AM, apneadiving wrote:

> As I am writing a brand new API, I’d like to test it’s response.  
>   
> Basically, it would be great in my case to test if ActiveResource gets  
> the expected data but it means I have to launch a test server in  
> background for the whole suite.  
>   
> Is there a convenient to handle this? Currently, I launch the test  
> server in console but it would be much better to have this handled  
> programmatically.

I’d recommend you check out [GitHub - rack/rack-test: Rack::Test is a small, simple testing API for Rack apps. · GitHub](https://github.com/brynary/rack-test). It  
provides access to attributes of the request and the response object,  
and obviates the need for running a server. Take a look at its specs:  
[rack-test/spec/rack/test\_spec.rb at main · rack/rack-test · GitHub](https://github.com/brynary/rack-test/blob/master/spec/rack/test_spec.rb).

HTH,  
David

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [January 14, 2012, 5:47pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/3 "2012-01-14T17:47:05Z")

</div>

On Jan 14, 2012, at 10:32 AM, David C. wrote:

> > programmatically.
> 
> I’d recommend you check out [GitHub - rack/rack-test: Rack::Test is a small, simple testing API for Rack apps. · GitHub](https://github.com/brynary/rack-test). It provides  
> access to attributes of the request and the response object, and obviates the need  
> for running a server. Take a look at its specs:  
> [rack-test/spec/rack/test\_spec.rb at main · rack/rack-test · GitHub](https://github.com/brynary/rack-test/blob/master/spec/rack/test_spec.rb).

Of course, you get rack-test for free if you use request specs (which  
wrap Rails integration tests, which use rack-test).

> **[RubyDoc.info: 
      
  File: README
  
    – Documentation for rspec-rails...](https://rubydoc.info/gems/rspec-rails/file/README.md#Request_Specs)**
>
> Documenting RubyGems, Stdlib, and GitHub Projects

> **[6. Integration Testing - Testing Rails Applications — Ruby on Rails Guides](https://guides.rubyonrails.org/testing.html#integration-testing)**
>
> Writing automated tests can be a faster way of ensuring your code continues to work as expected than manual testing through the browser or the console. Failing tests can quickly reveal issues, allowing you to identify and fix bugs early in the...

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [January 15, 2012, 4:18am UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/4 "2012-01-15T04:18:43Z")

</div>

Agreed. This is even more a bullet to bite early if your app  
orchestrates  
over more than one service, or if services talk to other services or  
both.

Testing this kind of setup is pretty difficult and the tests are  
typically  
very brittle.

Best,  
Sidu.  
[http://c42.in](http://c42.in)  
[http://rubymonk.com](http://rubymonk.com)

Sent from my phone

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [January 15, 2012, 2:49am UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/5 "2012-01-15T02:49:24Z")

</div>

On Sun, Jan 15, 2012 at 3:36 AM, David C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the expected data but it means I have to launch a test server in  
> It provides access to attributes of the request and the response object, and  
> obviates the need for running a server. Take a look at its  
> specs: [rack-test/spec/rack/test\_spec.rb at main · rack/rack-test · GitHub](https://github.com/brynary/rack-test/blob/master/spec/rack/test_spec.rb).
> 
> Of course, you get rack-test for free if you use request specs (which wrap  
> Rails integration tests, which use rack-test).

This could be considered to be off topic, but rack-test was advised…

Some think this issue is a trap for young players, others that it is  
just a different test philosophy. Nonetheless:

After a while you will eventually face the fact that you _have_ to  
test responses from your whole ‘stack’, specifically all the  
interactions of the different components, and rack-test does _not_ do  
this.  
Better to bite this bullet early.  
FWIW I have unit tests that are just that, and integration tests that  
are, well, integration tests - which precludes (by definition) using  
rack-test 🙂  
To get interagtion tests running (quickly) you’ll likely need to use  
Vagrant+Chef+VCR, or some such toolbox, to launch your test stack  
(locally, AWS, Rackspace etc.)

Again, this is inevitable, so better to bite integration testing early  
while things are simple - build the complexity incrementally over  
time.

From someone who years ago wasted too many days trying to isolate  
issues that rack-test happily passed.

Now at least you can’t say your weren’t forewarned 😉

HTH

> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)  
> –  
> πόλλ’ οἶδ ἀλώπηξ, ἀλλ’ ἐχῖνος ἓν μέγα  
> [The fox knows many things, but the hedgehog knows one big thing.]  
> Archilochus, Greek poet (c. 680 BC – c. 645 BC)  
> [http://hedgehogshiatus.com](http://hedgehogshiatus.com)

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [April 21, 2012, 10:16pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/6 "2012-04-21T22:16:05Z")

</div>

How one should test an API which depends on other web services (SOAP  
etc). e.g. Login to my API internally calls to another webservice API  
for authentication. Also all the data sent back by my API is actually  
data retrieved from another web service. Also should we verify the data  
or just responses are 200 or something else.

Any thoughts?

Sidu P. wrote in post #1040936:

> Agreed. This is even more a bullet to bite early if your app  
> orchestrates  
> over more than one service, or if services talk to other services or  
> both.
> 
> Testing this kind of setup is pretty difficult and the tests are  
> typically  
> very brittle.
> 
> Best,  
> Sidu.  
> [http://c42.in](http://c42.in)  
> [http://rubymonk.com](http://rubymonk.com)
> 
> Sent from my phone

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [January 18, 2012, 12:20am UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/7 "2012-01-18T00:20:34Z")

</div>

Thanks for all your answers,

I should have said I’m very used to the workflow of integration specs  
and capybara.

My question is really oriented towards API testing and particularly on  
the use of Active Resource.

Basically, I’d like to:  
a- create ActiveResource classes in rspec/support  
b- run my tests

for a, I don’t really know what URL I should provide  
for b, according to what you said, this kind of tests should live in  
rspec/requests

Any more enlightenment? 🙂

Ben

---

<div class="post-metadata">

**Author:** ![apneadiving](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@apneadiving](https://www.ruby-forum.com/u/apneadiving)\
**Post date:** [June 18, 2013, 6:07pm UTC](https://www.ruby-forum.com/t/testing-an-api-im-creating-need-test-server-to-be-running/215749/8 "2013-06-18T18:07:49Z")

</div>

For mocking other web services based on REST (ActiveResource) I use Dupe  
gem.  
([GitHub - moonmaster9000/dupe: BDD your (ActiveResource compatible) services from the client-side, before they exist.](https://github.com/moonmaster9000/dupe)).  
It is similar to FactoryGirl.
