Getting Started with RSpec — Part 1

Gems are Ruby libraries developed by other programmers that quickly add functionality to a program. Gems are primarily used with Ruby on Rails applications, so if you get a little lost it’s okay. Just try to follow along.

# calculator/Gemfile
source “"
gem ‘rspec’, ‘~> 3.6’
calculator $ bundle install
Fetching gem metadata from
Fetching version metadata from
Resolving dependencies…
Using bundler 1.15.3
Using diff-lcs 1.3
Using rspec-support 3.6.0
Using rspec-core 3.6.0
Using rspec-expectations 3.6.0
Using rspec-mocks 3.6.0
Fetching rspec 3.6.0
Installing rspec 3.6.0
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
# calculator.rb
class Calculator
def add(x, y)
x + y
calculator $ irb: 001 > load ‘./calculator.rb’
=> true
: 002 > calc =
=> #< Calculator:0x007f9ea882cb40>
:003 > calc.add(2, 3)
=> 5
require ‘./calculator.rb’
require ‘./calculator.rb’describe “calculator” do
it “adds numbers”
calculator $ rspec

calculator $ rspec
Pending: (Failures listed here are expected and do not affect your suite’s status)1) calculator adds numbers
# Not yet implemented
# ./spec/calculator_spec.rb:4
Finished in 0.00172 seconds (files took 0.08711 seconds to load)
1 example, 0 failures, 1 pending

require ‘./calculator.rb’
describe “calculator” do
it “adds numbers” do
calc =
expect(calc.add(2, 3)).to eql(5)
calculator $ rspec
Finished in 0.00523 seconds (files took 0.08796 seconds to load)
1 example, 0 failures
# calculator.rb
class Calculator
def add(x, y)

calculator $ rspec
1) calculator adds numbers
Failure/Error: expect(calc.add(2, 3)).to eql(5)

expected: 5
got: “ADD”

(compared using eql?)
# ./spec/calculator_spec.rb:6:in `block (2 levels) in <top (required)>’
Finished in 0.01897 seconds (files took 0.09196 seconds to load)
1 example, 1 failure
Failed examples:rspec ./spec/calculator_spec.rb:4 # calculator adds numbers



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