DEV Community

Liz Laffitte
Liz Laffitte

Posted on

Learning RSpec: First Tests for a Rails App

What the hell is RSpec?

Since day 2 of learning Ruby, I have heard about RSpec, RSpec, RSpec. I knew it had something to do with testing Ruby code, and that I should probably learn it. But what is it exactly?

The (RSpec website)[https://rspec.info/] itself is pretty vague on what RSpec IS but it’s pretty clear on what it does, and why it was made. RSpec is made for behavior-driven development (BDD, which apparently stems from test-driven development. In other words, write a test that describes a behavior you’d like your code to have, code that behavior, pass the test. Your tests drive your development.

The rspec-rails GitHub (README)[https://github.com/rspec/rspec-rails] gives us another morsel:

rspec-rails brings the RSpec testing framework to Ruby on Rails

RSpec is also described online, in various places, as a domain-specific language testing tool, a unit test framework and a BDD tool.

This is how I got started with RSpec.

The first step was to install both the core and the Rails RSpec gems.

$gem install rspec rspec-rails
Enter fullscreen mode Exit fullscreen mode

Next, I added the dependencies to my Gemfile, under my development and test group.

group :development, :test do
  gem 'rspec-rails', ">= 3.9.0"
end
Enter fullscreen mode Exit fullscreen mode

I then ran:

$rails generate rspec:install
Enter fullscreen mode Exit fullscreen mode

This generates the general spec directory with two files: rails_helper.rb and spec_helper.rb.

To keep my spec files organized, I created a models directory and created a specific model spec file for my first model: course_sepc.rb.

I started with writing tests that would check the validity of my course model.

/spec/models/course_spec.rb

require 'rails_helper'

RSpec.describe Course, :type => :model do
    subject {described_class.new(
                name: "Python Basics", 
                description: "Learn the building blocks of the wonderful general purpose programming language Python.", 
                difficulty: "Easy", 
                topic:"Python")
            }
    it "is valid with valid attributes" do
        expect(subject).to be_valid
    end
    it "is not valid without a name" do
        subject.name = nil
        expect(subject).to_not be_valid
    end

    it "is not valid without a description" do
        subject.description = nil
        expect(subject).to_not be_valid
    end

    it "is not valid without a difficulty" do
        subject.difficulty = nil
        expect(subject).to_not be_valid
    end

    it "is not valid without a valid difficulty" do
        subject.difficulty = "Easy Enough"
        expect(subject).to_not be_valid
    end

end
Enter fullscreen mode Exit fullscreen mode

First we indicate that we are describing Course, which is a model. We then define a subject in the group scope to be used in our individual tests. Then we declare examples using the it method. In each nested example, except the first, we are changing the value of one of our subject’s attributes. We are then expressing that we expect the subject to not be valid.

Describe, it and expect are all RSpec keywords. Describe defines and holds a group of tests. It defines an example or a test. Expect defines…an expectation.
Duh

To run your tests, enter the command:

$bundle exec rspec
Enter fullscreen mode Exit fullscreen mode

And there you have it! My first RSpec tests. Stay tuned for my exploration with Cucumber!

Top comments (3)

Collapse
 
eclecticcoding profile image
Chuck

Great article. I wrote an article a few month ago about setting up testing environment. Take a look.

Collapse
 
joaquinm91 profile image
Joaquin Marquez

Thanks , its a good guide to start learning about automated tests in ruby on rails :)

Collapse
 
lizlaffitte profile image
Liz Laffitte

Thanks for reading! If you find any other guides, let me know!