Unit testing is a crucial part of the software development process as it helps to assure the quality and dependability of the code. The well known ruby on rails web application framework offers an excellent testing framework called RSpec that makes it simple to create and run unit tests. In this article, we'll go over the processes for setting up unit testing in Ruby on Rails using RSpec.
Installing RSpec
The first step to start with Rspec is to install the Rspec gem. To achieve that, add the below line into your Gemfile.
group :development, :test do
gem ‘rspec-rails''
end
Now in the command line, run bundle install
to install the gem.
Next, generate the Rspec configuration files by running the below command:
rails generate rspec:install
After running the command, a ‘spec’ directory will be created in the root of your application along with some default configuration files.
Writing First Test
After installing the Rspec in our application, we are ready to go with writing the first unit test. Let’s suppose you have a model named ‘Link’ in the rails application that you need to test. First step is to create the test file for the model. To do that, run the following command.
rails g rspec:model Link
It will create a folder ‘model’ inside the spec directory. Inside the folder, a file named ‘link_spec.rb’ will be present. Now that we have created our first test file, Let’s see what’s inside the file.
require 'rails_helper'
RSpec.describe Link, type: :model do
pending "add some examples to (or delete) #{FILE}"
end
In this file, describe method represents the class or module you want to test. Inside the method, you can write individual test cases using the ‘it’ method, followed by a description of what you are testing.
Let’s write a test to check if a link is created and returned properly.
require 'rails_helper'
RSpec.describe Link, type: :model do describe 'create' do it 'creates a new link successfully' do link = Link.new(title: 'Test link', url: 'www.testlink.com') expect(link.save).to be_truthy end end end
In this test, we created a new ‘Link’ Object with a Title of ‘Test link’ and Url of ‘www.testlink.com’. We then use the ‘expect’ method to define an expectation that it returned true or not after saving the link. The ‘to be_truthy’ method is used to check if the result which is true matches the expected result.
Run your tests
Once we have written the test, it’s time to run it. The test can be run by writing the below command in the command line.
rspec spec/models/link_spec.rb
This will execute the test in the link_spec.rb
file and display the results in your terminal. If the test pass, you should see a green output, indicating that your code is functioning as expected. If any test fail, you will see a red output, indicating that there are issues with your code that need to be addressed.
Writing more tests
You can continue to write more tests for your models, controllers, views and other parts of your Rails application using RSpec. RSpec provides a rich set of matchers and assertions that you can use to test various aspects of your code, such as checking for expected behavior, handling edge cases, and validating input/output. For example, you can write tests to check if a link is valid or not, if a user is authenticated after signing it, if a controller action redirects to the correct page and so on.
The above article gives you a basic understanding of how we can set up Rspec for testing our app and add the test as per the requirement.