Simple Rails API Server using Simple TDD
modified from https://scotch.io/tutorials/build-a-restful-json-api-with-rails-5-part-one
Hello, on this evening I want to share about how to create a simple rails API server using Test Driven Development Method.
TDD (Test Driven Development) is one approach that has grown on the Rails community. Although, not completely accepted by all in the community. This approach has you writing your test(s) first and then writing code to make your tests pass.
The goal of this article is creating a simple CRUD rails API server. I found out the tutorial on scotch.io is too advanced for a beginner so I decided to make it more simple for newbies. Okay let’s ride along..
We’re gonna create a set of endpoint which are:
Endpoints:
GET /articles
GET /articles/:id
POST /articles
PUT /articles/:id
DELETE /articles/:id
let’s create our app $ rails new our_app_name -T -a --database=postgresql
We need to add these gems to our Gemfile.
- rspec-rails — Testing framework.
- factory_bot_rails — A fixtures replacement with a more straightforward syntax. You’ll see.
- shoulda_matchers — Provides RSpec with additional matchers.
- database_cleaner — You guessed it! It literally cleans our test database to ensure a clean state in each test suite.
- faker — A library for generating fake data. We’ll use this to generate test data.
after installing our gems with $ bundle install then we do $ rails generate rspec:install , This adds the following files which are used for configuration:
.rspec
spec/spec_helper.rb
spec/rails_helper.rb
Let’s modify spec/rails_helper.rb to configure our gems.
MODEL
We can start creating our Article model by executing $ rails generate model article title:string content:text.
After creating the database $rails db:create, then run migration $ rails db:migrate.
We’re gonna write our Model test in spec/model/article_spec.rb, on this step we use shoulda matchers gem for our model test.
Then run rspec $ bundle exec rspec and the Test is surely failling.
Lets Fix it , modifying app/model/article.rb.
run $ bundle exec rspec again, and model test error is gone.
Lets create our test data using Faker gem, define spec/factories/article.rb and By wrapping faker methods in a block, we ensure that faker generates dynamic data every time the factory is invoked.
CONTROLLER
Okay, let’s create our articles controller by running $ rails generate controller article.
We use request spec since we’re gonna test an api controller as stated on https://github.com/rspec/rspec-rails.
Use request specs to describe the client-facing behavior of the application — specifically, the HTTP response to be issued for a given request (a.k.a. integration tests). Since such client-facing behavior encompasses controller actions, this is the type of spec to use for controller testing.
Now create our test suite on spec/request/article_spec.rb.
We initialize 10 test data using faker gem, and we’re also using json helper that defined in spec/support/article_spec_helper.rb.
To enable support directory, let’s modify spec/rails_helper.rb file.
Run the test and we’ve got a lot of error, let’s fix routing error first.
Finally we’re going to define our Articles controller.
We use json_response.rb and exception_handler.rb helper, let’s define it.
Include helpers file to application_controller.rb so our controller will know it.
Run the test, we’ve got all green! (Hopefully)
That’s it!
You can find the repo here: https://github.com/pascalesdedy/latihan-rails-api-TDD