Last active
August 29, 2015 14:07
-
-
Save johnwake/12ea4291ecacc1cee0a7 to your computer and use it in GitHub Desktop.
Example Rails API test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'Tasks API' do | |
before :each do | |
FactoryGirl.create :project | |
Project.last.integrations << Integration.last | |
end | |
# GET /tasks/:id | |
it 'should return a single task' do | |
api_get "tasks/#{Task.last.id}", {token: Integration.last.user.api_key.token} | |
response.status.should == 200 | |
project = JSON.parse(response.body) | |
project['id'].should == Task.last.id | |
project['project_id'].should == Task.last.project_id | |
project['source_name'].should == Task.last.source_name | |
project['source_identifier'].should == Task.last.source_identifier | |
project['current_state'].should == Task.last.current_state | |
project['story_type'].should == Task.last.story_type | |
project['current_task'].should == Task.last.current_task | |
project['name'].should == Task.last.name | |
end | |
end | |
################## API HELPER ######################### | |
def api_get action, params={}, version="1" | |
get "/api/v#{version}/#{action}", params | |
JSON.parse(response.body) rescue {} | |
end | |
def api_post action, params={}, version="1" | |
post "/api/v#{version}/#{action}", params | |
JSON.parse(response.body) rescue {} | |
end | |
def api_delete action, params={}, version="1" | |
delete "/api/v#{version}/#{action}", params | |
JSON.parse(response.body) rescue {} | |
end | |
def api_put action, params={}, version="1" | |
put "/api/v#{version}/#{action}", params | |
JSON.parse(response.body) rescue {} | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment