Last active
March 6, 2023 07:13
-
-
Save muripic/6a3a980dce3a035ea3ed791fdaad34d4 to your computer and use it in GitHub Desktop.
Grape endpoint example (and test with minitest)
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
| # 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 |
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
| # 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