Testing Your Rails API Controllers

Vakas Akhtar
3 min readJan 26, 2021

Test driven development (TDD) allows developers to ensure that they’re on the right track in their development roadmap. In this article I’ll go over some basic Rspec tests that can be used for your controllers in your Rails API. Before we start add the ‘rspec-rails’ gem to your gemfile. The run

bundle 

in your terminal to install it. Now you go ahead and run

rails generate rspec:install

This will then create a spec directory and a .rspec file. From here we can create our tests that we’ll run. It’s important to note that the tests you will run in your controller it uses a separate database that must be seeded with its own unique data. Now lets begin writing some tests. Say we had a controller in our api that retrieves a JSON object that had a list of transactions. Let’s write a test that makes sure the route returns an OK status. Inside the spec directory a new file and name it transactions_rspec.rb. Inside this file write out the following code.

require 'rails-helper'describe 'My API', type: :request do 
it 'returns all transactions' do
get '/api/v1/transactions'

expect(response).to have_http_status(:success)
end
end

Now to run your tests simply run this in your terminal.

rspec

Now you should be able to see whether or not your route is returning a proper status. Next up we’ll make sure that the response body is a proper size. To do this just add one more line of code below the first test that checked for the route’s status.

require 'rails-helper'describe 'My API', type: :request do 
it 'returns all transactions' do
get '/api/v1/transactions'

expect(response).to have_http_status(:success)
expect(response.body.size).to eq(2)
end
end

Notice that we’re only checking for the response body size without parsing the the response. If we were to parse the response as a JSON object it would return nothing as there is no test data regardless of if you seeded your API’s Postgres, SQLite database, or whatever your are using. Rspec uses its own database to test so we’ll need to create some dummy data to properly test it. Add the ‘factory_bot_rails’ gem to your Gemfile to get started. You can read the documentation for the gem here. Now run bundle to install it and create a new folder inside the spec directory called factory. Inside the factory directory create a file called Transaction.rb. Inside this file enter the following code.

FactoryBot.define do 
factory :transaction do
end
end

Now go back to your transactions_spec.rb file and create your test transactions and then change the test to parse the body and check that the JSON object returns two objects.

require 'rails-helper'describe 'My API', type: :request do 
it 'returns all transactions' do
FactoryBot.create(:transaction, item: "Water Bill", type_trans: "Expense", category: "Bills/Utilities", amount: 90.46, date: '2021-01-24')
get '/api/v1/transactions'

expect(response).to have_http_status(:success)
expect(JSON.parse(response.body).size).to eq(1)
end
end

Now we’ve successfully created a factory bot to be used as our test data and our second test is expecting to see that single transaction that’s been created. Remember to check the schema to make sure that the FactoryBot created has all the proper and required attributes. Now we have a successful test case with some Rspec test data. Now with the fundamental concepts from this testing process and tools that we’ve used go ahead and test the rest of your controllers.

--

--