Test driven development is great if you would like to have an application that is robust as well as flexible. Your write a test that fails, then you write the minimum code to get that test passed, and the third & final step, you refactor that code.

As Rails comes pre-built with a lot of good practices, so, strong capabilities for Test Driven Development in Rails is of no surprise. Rails comes packaged with MiniTest framework, but usually it’s a beginner choice. A lot of developers recommend it, but for an intermediate developer, who is to write a lot of stuff, and obviously a lot of test code along the way as well, RSpec is a framework of choice.

RSpec is a testing framework for Rails, that uses DSL: Domain Specific Language. It contains numerous methods to test different things in Rails like Models, Controller, as well as Integration of different components together like testing the flow of your application. It’s pretty easy to install and use. So let’s see how can you get up and running with RSpec.

First, we need to install RSpec gem, and it’s merely a single line of code in Gemfile.

gem "rspec-rails"

We can do in a better way, since the testing stuff is needed only in two environments: ‘development’, and ‘testing’, so we can write the same information in Gemfile:

gem "rspec-rails", group: [:development, :test]

Running bundle install will install the gem for us, along with all the required dependencies.

Running the following command will generate the basic files for RSpec:

rails generate rspec:install

Now, every model we are going to generate in our application, will by default contain the accompanying test files with it.

Let’s generate a model called User with the following options:

rails generate model User name:string age:integer

Don’t forget to invoke rake db:migrate in order to apply the changes in your database.
It’s gonna generate a directory called spec that is going to contain the stuff related to tests generated by RSpec.

Generating a model will generate an associated file called user_spec.rb inside models directory that reside inside spec .

Here is the content of user_spec.rb :

require 'rails_helper'

RSpec.describe User, type: model do
pending "add some examples do (or delete) #{__FILE__}"

We lay down individual tests into examples. For each test, we write an example to test a single feature for a model.

Since, we have not written any validation for our user model, so instantiating a new user object, and calling .valid? on it should return true, and this is something that we would like to get verified through testing.

it "should be valid with no name and age" do 
  user = User.new
  expect(user.valid?).to be true
end

We can be a little fancy, and use something like this:
expect(user.valid?).to be_truthy , anyway both lines server the same purpose: try to checkout user.valid? returns true.

Now, it’s finally the time to run our test, and we can do so by invoking the following command:

rspec spec/models/user_zombie.rb

If all goes well, it wil return you the following output:

1 example, 0 failures

So in the first part, we looked into how to install RSpec, and how to get it working with Rails. In the very next article, we’ll dive into specifics of testing through RSpec. Happy testing!

Leave a Reply

Your email address will not be published. Required fields are marked *