Khoa Nguyen
Khoa Nguyen

Reputation: 1600

testing Rails json API

I have a Rails app that serves up JSON, and want to test some code that request and process some json via http. What's the best way to test this code without stubbing http request-response?

I'm thinking of firing webrick to serve Rails app, but technically inept to do so. Any pointer to code example is appreciated.

# lib/bam.rb
require 'open-uri'

class Bam
  def bam host='localhost'
    hash = JSON.parse( open("http://#{host}/bam.json) )
    # process hash
  end
end

# spec/bam_spec.rb
RSpec.describe 'Bam' do
  before(:all) do
    # fire up webrick
  end
  after(:all) do
    # shutdown webrick
  end

  it 'bam' do
    hash = Bam.new.bam
    hash.should eq('bam' => 'bam')
  end
end

Upvotes: 0

Views: 68

Answers (1)

Sivakumar
Sivakumar

Reputation: 1119

You can use airborne gem https://github.com/brooklynDev/airborne. Also you can compare JSON structure.

Upvotes: 1

Related Questions