KayleL
KayleL

Reputation: 2873

Testing an API built on a rails environment

Currently I am making an open API for my website. Since all the information available through the API is not protected, I am not bothering with tokens, thus making the development of the API a hell lot more easier on my part.

I am very new with APIs, thus I am not sure what developers expect to retrieve, and how they would retrieve it. Currently, the API is tailored to the purposes of my own website. I want to know how to test my API that is generated through my Rails development environment, on other types of environments.

Upvotes: 0

Views: 113

Answers (1)

Whitney Imura
Whitney Imura

Reputation: 810

I'd consider using Cucumber to test your APIs..I'm kinda unclear about what exactly you're trying to do, but I test APIs inprocess through Cucumber (automated testing).

You can write your cucumber steps using any language, but I'd recommend writing them in the same language as your app (so, ruby).

You can use FactoryGirl (FactoryGirl.create) to create objects to plug into your DB and use DatabaseCleaner to delete the objects in the db automaticlaly.

You'll find that a lot of documentation uses Sinatra to mock your app, but I don't think that's a good practice. Instead, I plug use my rails app directly by using this code in my env.rb file:

FactoryGirl.definition_file_paths = %w(custom_directory)
FactoryGirl.find_definitions

**module AppHelper
  def app
    YourAppApi::Application
  end
end
World(Rack::Test::Methods, AppHelper)**

You also need to make sure you require the following:

require ::File.expand_path('../../../config/environment',  __FILE__)
require 'cucumber/rails'
require 'factory_girl_rails'
require 'rack/test'

The rest (feature/step_definitions) is easy....You can see them/examples at my blog @ whitneytaylorimura.wordpress.com

Upvotes: 1

Related Questions