[ BNCC x GOJEK ACADEMY ] Validation and Unit Testing in Rails

Wenn
6 min readOct 11, 2018

--

Hi! Welcome back to my medium. As usual, I will share lessons that I and my friend learned in previous Software Engineering Bootcamp with GOJEK. In this article, I am going to share the lesson about validation and unit testing in Ruby On Rails.

In our software engineer bootcamp with GOJEK, we have been building a simple application of Go-Food (Food Ordering system like GOJEK application) using Ruby on Rails. Previously, we have already build Go-Food System using Scaffold command in Ruby on Rails which it automatically generates Create-Read-Update-Delete operations in our application together with the model (database), view and controller itself. Related to our topic in this article, we are going to add data validation to our Go-Food systems.

In software development, it is essential for us to validate the data that are going to stored in the database. Validation is important for us to ensure our application operates on clean, correct and useful data stored in the database. For a simple example, we expect to store user email in our database. Therefore, we supposed to check that the email before stored in our databases, it matches the standard email format to prevent useless spams of invalid email stored in our database. Back to our Go-Food system, we are going to validate name, description, image link and price of food before it is stored in our database.

Here’s is our expected rule for validation of data before it is stored to database:

a. The name field is required which means it cannot be empty (cannot be nullable)

b. Name field should be unique which means we can’t input the same name of food into our database

c. Description field too is required and cannot be empty

d. Price field should only store numeric values

d. Image link should only store image file (.png / .jpg / .gif) and image URL may have empty value

The next thing we are going to do is to make sure our Food model has been generated in our Rails application. If you still don’t have Food model generated, you can type the command below in your terminal (make sure you run the command inside the directory/folder of your rails application)

The previous command will automatically generate Food model and migration, view and controller in your rails application. But we are not done yet, we still need to migrate our food database migration file to our PostgreSQL / SQLite by using the command below

You can see the result of the previous command in localhost:3000/foods (make sure you already start your server by running the ‘rails server’ command in the terminal).

Now, we are going to validate the field rules stated before in our Food model. You can follow to write the rails code below in your foods.rb

Explanation of code:

In the first line of our model validation, it validates the specified name and description field cannot be empty / nil (the value of data should presence) when it attempts to store in our database. Next line, it validates the price field have only numeric values and ‘greater_than_equal_to’ constraints to specify the value stored must be greater than or equal to the supplied value (0.01). The third line validates the name field should only store one value, it doesn’t allow the same name to store in the database. The final line validates the image URL stored value by verifying it follows the Regular Expression format in which should end with “gif/jpg/png” file extension and it allows image URL to be nullable/empty when stored in the database. Here’s the official documentation (guides.rubyonrails.org/active_record_validations.html) from Rails for you to learn more about validation syntax

After we validate the data before it is stored in the database, we also need to run unit testing to our application before it goes to into production (deploy and use by the real user). Unit testing is a level of software testing where each units/components of application are tested if it performs the function as it’s designed for. In this situation, the unit we are going to test is data validation function in our model to make sure our validation works.

In Ruby on Rails, we are going to use a unit testing tool provided in rails called RSpec. So first, you need to make sure RSpec and tools needed for testing is included in your Gemfile. You can add the following code into your Gemfile

Explanation of Gems you are going to install in your Rails:

a. Rspec-Rails: Unit testing tools for Rails application (documentation: github.com/rspec/rspec-rails)

b. Factory girls rails: Helper tool as fixtures (default Rails tool for generating sample data) replacement (documentation: github.com/dscape/factory_girl_rails)

c. Faker: Helper tools to generate random fake test data for you (documentation: github.com/stympy/faker)

d. Capybara: Helper tools to simulate user story. In our case, it simulates user input data. (documentation : github.com/teamcapybara/capybara)

e. Database-cleaner: helper tools to make sure our testing database is in a clean state for each RSpec session (unit testing) (documentation: github.com/DatabaseCleaner/database_cleaner)

f. Selenium-web driver : helper tools that come together with Capybara which allow us to test javascript-based interactions (front-end) like AJAX request (documentation: https://github.com/SeleniumHQ/selenium/tree/master/rb)

Before we run the unit testing with RSpec , we need to make sure our test database already set up. You can check it out in “config/database.yml”. Here’s the example of the database.yml with test database set up

Next, we are going to generate RSpec file by the command below in terminal

And the last config , we are going to modify “config/application.rb” as below

When everything is already set up, the next thing we are going to run a simple unit testing with RSpec. First, we need to create food.rb in the folder “spec/factories/food.rb” with the line below

Then, we need to add code to our Spec file as below

Then, we execute “rspec –format documentation” command in our terminal and the terminal will return the information of “1 example, 0 failures” which means you successfully run unit-testing with RSpec with your Rails application! You can check out in Rspec documentation for more details about RSpec and Unit testing.

That’s all from me. Thank you for reading!

--

--

Wenn
Wenn

Written by Wenn

Google Certified Android Developer. Learning about Android, Backend technology and Algorithms.

No responses yet