How to Write Rspec for Model Testing in Rails Applications.

By: Shivani Nerkar Category: Ruby on Rails Technologies: Ruby on Rails

Understanding RSpec:

In RSpec, the ‘R’ stands for Ruby and ‘Spec’ stands for Specification. A testing tool framework, RSpec is formulated in Ruby programming language to test the Ruby code. RSpec is a Behaviour-Driven Development (BDD) framework that is widely used in production applications by Ruby programmers. BDD employs a unique approach to software development by combining Domain-Driven Designs with Test-Driven Development and Acceptance Test-Driven Planning. RSpec assists users to perform the TDD by focusing on several aspects of documentation and design.

Steps for drafting RSpec for Model Testing:

Step 1: rails new RSpec_app_demo

Step 2: Add gem in gemfile “gem RSpec-rails”

Note: if using Rails 6, the user needs to write: “gem rspec-rails,4.0.0.beta2”

Step 3: bundle install

Step 4: rails generate rspec:install

It generates the spec folder in the application.

Step 5: rails generate Movie title director rate:integer

Step 6: rails db:create db:migrate

Step 7: The user can write the testing scenarios in the spec/model/movie_spec.rb.

Step 8: Add the validation for the model in the model.rb file.

Step 9: Run “rspec” on the terminal.

Step 10: The user can check the tested data in the terminal, which the user writes as scenarios in model id to check if they fail or not.

Add validations in movie.rb

After running any of these commands, the console should resemble as following:

Conclusion:

Outputs like this help to quickly gain a higher understanding of how the object behaves. A comprehensive behavior-driven design process tool, RSpec enables users to write human-readable specifications to direct and validate the application development.

Originally published at https://www.cryptextechnologies.com.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cryptex Technologies

Cryptex specializes in developing open source web applications and software solutions across all domains and verticals using Ruby on Rails (ROR) technology