Writing a Simple RSpec Test

matthewbritt

Matt Britt

Posted on February 14, 2020

Writing a Simple RSpec Test

RSpec is a popular testing framework for Ruby. Here we will write some simple tests for ourselves to help learn the basics.

The first step is to install our gem using

$ gem install rspec

Then, inside of our project directory, we call

$ rspec --init

This initializes two files: the spec_helper and the .rspec file. We will also need to create a spec file in which to build our tests. Today this will be named spec/cat_spec.rb. We are building instances of the class Cat inside of a file we have named lib/cat.rb.

Inside of spec/cat_spec.rb, we need to

require "spec_helper"

which will load the configuration for the specification. We will also

require "cat"

as this is the class for which we will be writing examples. We can define this class inside of our lib/cat.rb file.

class Cat
end

We are then ready to describe how we want our cats to behave. We start by writing a "describe" block in spec/cat_spec.rb. Inside of this block we will write our examples to test.

require "spec_helper"
require "cat"


describe Cat do
    #examples to test    
end

Here our description will test for the name of our cat instance.

describe Cat do
    it "is named Daz" do
        cat = Cat.new
        expect(cat.name).to eq("Daz")
    end
end

We can run our test by calling $ rspec.
Doing so now gives us a failing test.


  1) Cat is named Daz
     Failure/Error: expect(cat.name).to eq("Daz")

     NoMethodError:
       undefined method `name' for #<Cat:0x00007f948d0e7030>
     # ./spec/cat_spec.rb:8:in `block (2 levels) in <top (required)>'

Finished in 0.0041 seconds (files took 0.12837 seconds to load)
1 example, 1 failure

We can then write code in our lib/cat.rb file to correct the

undefined method `name'

and get our test to pass.

class Cat
    attr_accessor :name
    def initialize
        @name = "Daz"
    end
end

Running $ rspec again should give us a green light on our test.

Now that we have one test passing, let's write another description to test in spec/cat_spec.rb.

describe Cat do
    it "is named Daz" do
        cat = Cat.new
        expect(cat.name).to eq("Daz")
    end
    it "is the color orange" do
        cat = Cat.new
        expect(cat.color).to eq("orange")
    end
end

Running $ rspec now should show that we have 2 examples and are failing 1 of them.

1) Cat is the color orange
     Failure/Error: expect(cat.color).to eq("orange")

     NoMethodError:
       undefined method `color' for #<Cat:0x00007ff2fa9a3248 @name="Daz">
     # ./spec/cat_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.00502 seconds (files took 0.1164 seconds to load)
2 examples, 1 failure

If we take direction from the test and add code that defines our cat color as orange, we should be able to get a second passing test.

class Cat
    attr_accessor :name, :color
    def initialize
        @name = "Daz"
        @color = "orange"
    end
end
Finished in 0.00474 seconds (files took 0.12366 seconds to load)
2 examples, 0 failures

And that's it. We can continue to practice with as many tests as we would like.

💖 💪 🙅 🚩
matthewbritt
Matt Britt

Posted on February 14, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Writing a Simple RSpec Test
beginners Writing a Simple RSpec Test

February 14, 2020