Skip to content

Instantly share code, notes, and snippets.

@muripic
Last active March 6, 2023 07:13
Show Gist options
  • Select an option

  • Save muripic/6a3a980dce3a035ea3ed791fdaad34d4 to your computer and use it in GitHub Desktop.

Select an option

Save muripic/6a3a980dce3a035ea3ed791fdaad34d4 to your computer and use it in GitHub Desktop.
Grape endpoint example (and test with minitest)
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'grape'
end
require 'grape'
require 'json'
class HelloWorldAPI < Grape::API
prefix 'api'
version 'v1'
format :json
get 'hello_world' do
{ Hello: 'World!' }
end
end
# Set up fake Rack application
builder = Rack::Builder.app do
run HelloWorldAPI
end
response = Rack::MockRequest.new(builder).get('/api/v1/hello_world')
puts response.status
puts response.body
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'grape'
gem 'minitest'
gem 'rack-test'
end
require 'minitest/autorun'
require 'rack/test'
require_relative 'example'
describe 'HelloWorld Grape endpoint' do
include Rack::Test::Methods
def app
HelloWorldAPI.new
end
describe 'GET /hello_world' do
it 'returns a 200 status code' do
get '/api/v1/hello_world'
assert last_response.ok?
end
it 'returns the correct message' do
get '/api/v1/hello_world'
expected_body = { Hello: 'World!' }
assert_equal expected_body, JSON.parse(last_response.body, symbolize_names: true)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment