How to create tests in Ruby on Rails? (Part 1)

Do you want to do TDD in Ruby on Rails? Learn to test is the first step.

Knowing how to create tests is one of the most appreciated skills in web development. However, testing can be a tedious job when you’re starting as a developer. How can you know if you’ve done enough tests? How do you know if your tests are failing because of you’re methods or due to your tests themselves?

For me, it has been a painful path to learn to test. I recognize the importance of the task. Each time that I’m advancing in a project, I want to be sure that each new method that I wrote it’s working as expected. But as I’m still a newbie, then I found that my tests are failing because I’m not writing good tests, not because my methods are failing.

Test-Driven Development: To test or not to test?

There’s not any dilemma: test ever. However, the question is when to test. Test-Driven Development is good practice for developers and every developer should learn to do TDD code as soon as possible. But it can be a nasty piece of work both newbies as experimented developers.

TDD is highly recommended as it’s highly criticized. Many developers compared it with communism: good in theory, but it doesn’t work. Or at least it doesn’t work for themselves.

I’ve experimented with that feeling in a project that I was developing for learning purposes. It was a web-app based on Ruby on Rails (RoR). I was new in Ruby, as on RoR, as on web-development. In resume, a complete newbie. And I read about how wonderful was TDD. So, I tried.

And all my tests failed.

The main problem was that I was learning to write tests. If I wanted to know if my tests were working, I needed to write some code and check if the code pass. And then, I should figure out if the tests were failing due to problems in my methods or problems in my tests.

My advice: if you’re learning how to do tests, don’t try TDD until you are sure how to test.

Saying this, here are some useful material to learn testing a web-app created with RoR.

Choice your testing framework

If you start a new Ruby on Rails application with the scaffold, you will have files ready to use to test your app. RoR cames with a Test::Unit support. However, few people use that framework.

Most of the people prefer RSpec. Install it in your web-app easily adding

gem 'rspec-rails'

in your Gemfile. Then, run

$ bundle install 

This will add RSpec to your web-app. The next step creates the files. This can be done with

$ rails generate rspec:install

If you see this message in the terminal, you’re doing everything right:

Running via Spring preloader in process 9219
      create .rspec
      create spec
      create spec/spec_helper.rb
      create spec/rails_helper.rb

Create your test models with

$ rails g rspec:model yourmodelname

This will give you the following output

Running via Spring preloader in process 10099
create spec/models/user_spec.rb

In the new file, you have a template for creating your tests.

Write your tests! You can start writing your validation and scope tests. Let’s use a validation test as an example.

RSpec.describe User, type: model do
context 'validations tests' do
it 'ensures first name presence' do
user = User.new(last_name: 'Last', email: 'sample@gmail.com').save
expect(user).to eq(false)
end
end
end

We define the context, then we describe what we’re testing (presence of the first name). As the first name is absent, the variable “user” should be false.

More examples in the next article!