Before implementing Rspec you should know what is the actual meaning of Rspec? R stands for ‘Ruby’ and the meaning of ‘Spec’ is Specification. RSpec is a unit test framework used in Ruby on Rails web application development. RSpec is a 'Domain-Specific Language' (DSL) testing tool. You should write Rspec in Ruby language to test your Ruby code.
Why do you use Rspec?
Rspec is an automated test case, used to test the code written by you while creating the application.
Developers follow the Rspec to write is known as TDD (Test Driven Development) and the Rspec written by the Testers, generally known as BDD (Behavior-driven development).
Advantages of Rspec:
There are several advantages of Rspec when you use it in your application.
How to set up Rspec in your Rails app?
Now to configure Rspec or to set up rspec in your application you should go through the following steps.
First, you need to add rspec-rails to both the :development and :test groups in the Gemfile:
1. group :development, :test do gem 'rspec-rails', '~> 3.7' end
2. Then you should run the command,
3. Then you should initialize the spec/ directory with:
rails generate rspec:install
This adds the following files which are used for configuration:
.rspec spec/spec_helper.rb spec/rails_helper.rb
If you want to add the files for your controller or model, then first you need to create the controller or model directory under spec folder and add the specific files inside those folders.
As an example you can say, spec/controllers/category_controller_spec.rb.
4. To run your specs you need to use the ‘rspec’ command.
i.e ~$ rspec
What is the basic syntax of Rspec?
Here describe is the Rspec Keyword, which is generally used to define a class name or an example group or string arguments. There are the basic building blocks to organize the tests.
RSpec.describe HelloWorld, type: :class do describe HelloWorld do context "when testing the helloworld123 class" do it "should say 'Hello World' when calling the method" do hlo= HelloWorld.new msg= hlo.say_hello expect(msg).to eq "Hello World!" puts "HelloWorld executed!" end end end end
The context keyword is the same as describe keyword. It also accepts string arguments or class names.
Here the context is a block that is used to describe the context, in which the class or method mentioned in the describe block is being used.
context "With valid input" do
it keyword basically defines what is the example do. In simple word, you can say, what kind of test is performed that is defined by it keyword. It is also used in a block of a statement and it contains do/end.
it 'create a new blog with a blog_name and description'
It defines what the expected output for the test is. It’s nothing but the expectations in rspec.
Sometimes need to use expect().to be true. It depicts the test result is expected to be true i.e positive test cases. Similarly, expect().to be false, it is expected the negative test results. RSpec uses a simple framework and keywords like should() and should _not() to express expectations.
Expect also use eql keyword i.e expect().to eq “hello”. Eql is basically a matcher that is used to show the equality of test results with the expected result.
At last, I hope this article will be helpful to understand and implement Rspec in your ruby code and to cover all the scenarios (i.e including positive and negative test cases).
For all the Ruby on Rails Developers, you can save a lot of your time while testing Ruby on Rails code making it faster. And overall it will be very much useful to write automated test cases rather than using manual test cases.