How to test a API?

https://github.com/vincentopensourcetaiwan/test_api

I create a project which let user manage cats via web interface, “/cats”, and provide api interface, “/api/cats”.

Now I can test my api by using curl, and following are the examples.

index

curl http://localhost:3000/api/cats.json

show

curl http://localhost:3000/api/cats/4.json

create

curl -d "cat[name]=api-new-record" http://localhost:3000/api/cats

update

curl -X PUT -d "cat[name]=api" http://localhost:3000/api/cats/4

destroy

curl -X DELETE http://localhost:3000/api/cats/4

How do i use Rspec to test my api, like i use curl?

Here is my project repository, hope it will improve the understanding.
https://github.com/vincentopensourcetaiwan/test_api

Many thanks for reply.

You can use RSpec to test your controllers:

require 'spec_helper'

describe Api::CatsController, 'GET index' do
  it 'returns a list of cats' do
    FactoryGirl.create_list(:cat, 2)

    get :index

    expect(response.body).to eq _your_expected_json_format_
  end
end

The other option is to have something like fdoc (GitHub - square/fdoc: Documentation format and verification) hook into your controller specs to validate the structure of your requests.

1 Like

Hi harlow,

Many thanks for your reply.

I try to follow your suggestion and use your codes.
So I install “rspec-rails” and “factory_girl_rails”, and create “spec/controllers/cats_controller_spec.rb”.
Following are my codes.

spec/controllers/cats_controller_spec.rb

require 'spec_helper'

    describe Api::CatsController, 'GET index' do
      it 'returns a list of cats' do
        FactoryGirl.create_list(:cat, 1)
    
        get :index
    
        expect(response.body).to eq 'Persian'
      end
    end

Gemfile

group :development, :test do
  gem 'rspec-rails', '~> 2.0'
end

group :test do
  gem 'factory_girl_rails'
end

I add following codes into “spec/spec_helper.rb”

config.include FactoryGirl::Syntax::Methods

spec/factories.rb

FactoryGirl.define do
  factory :cat do
    name 'Persian'
  end
end

When I execute “rspec spec/controllers/cats_controller_spec.rb”
I got following error message.

F

Failures:

  1) Api::CatsController GET index returns a list of cats
     Failure/Error: expect(response.body).to eq 'Persian'

       expected: "Persian"
            got: " "

       (compared using ==)
     # ./spec/controllers/cats_controller_spec.rb:9:in `block (2 levels) in <top (required)>'

Finished in 0.05409 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/controllers/cats_controller_spec.rb:4 # Api::CatsController GET index returns a list of cats

Randomized with seed 23347 

https://github.com/vincentopensourcetaiwan/test_api
This is my source codes.

Please help me to solve this. I am new to test-driven-rails.

Many thanks.

My apologies. I got the spec type wrong.

Its a Request spec that emulates a curl on your application.

Here is an example of a spec hitting the /api/cats endpoint:

# spec/requests/cats_api_spec.rb
require 'spec_helper'

describe 'Cat management' do
  it 'returns a list of cats' do
    # create a cat in the database with name Persian
    FactoryGirl.create(:cat, name: 'Persian')

    # make a get request to the cats index (note the format json)
    get '/api/cats', format: 'json'    

    # parse the json into ruby                        
    result = JSON.load(response.body)                  

    # asert that the cat returned from the API has the name Persian
    # note it's an array of hashes so i'm using ".first" on the array
    expect(result.first['name']).to eq 'Persian'          
 end

  it 'creates a cat' do
    # some test data to POST to the /api/cats endpoint
  end
end

This is a very simple example and you want to assert more about the returned cat. Hopefully this gets you going in the right direction.

1 Like

Hi harlow,

Many thanks for your reply.

The codes work great.

Thank you very much.