006 RailsClips Testing APIs with Faraday

This video goes over end-to-end testing of APIs when the setup is a little more involved or where Rack::Test won't work. Usually this requires deploying the API application to a server or heroku and setting up any services it depends on. Then we test the entire system front-to-back by making HTTP requests to the API endpoints using faraday. In this case, we're using json-server as an example API for testing. It requires that you have Node.js installed after which you can install json-server with npm.

$ npm install -g json-server

We copied the example db.json from the README of the json-server project and started json-server.

$ json-server --watch db.json

Then we wrote the tests with faraday making HTTP requests to the server endpoint and validating the result

require 'faraday' require 'json' describe "api access to posts" do let(:conn) {Faraday.new(url: "https://localhost:3000")} it "gets a list of posts" do resp = conn.get("/posts") posts = JSON.parse(resp.body) expect(posts.size).to eq(1) post = posts.first expect(post["title"]).to eq("json-server") expect(post["author"]).to eq("typicode") end it "gets a single post" do resp = conn.get("/posts/1") post = JSON.parse(resp.body) expect(post["title"]).to eq("json-server") expect(post["author"]).to eq("typicode") end end

Sign up for the Newsletter

Join our newsletter and get updates in your inbox. We won’t spam you and we respect your privacy.