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 “https://rubygems.org"
gem ‘rspec’, ‘~> 3.6’
calculator $ bundle install
Fetching gem metadata from https://rubygems.org/..........
Fetching version metadata from https://rubygems.org/.
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
end
end
calculator $ irb: 001 > load ‘./calculator.rb’
=> true
: 002 > calc = Calculator.new
=> #< Calculator:0x007f9ea882cb40>
:003 > calc.add(2, 3)
=> 5
require ‘./calculator.rb’
require ‘./calculator.rb’describe “calculator” do
it “adds numbers”
end
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 = Calculator.new
expect(calc.add(2, 3)).to eql(5)
end
end
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)
“ADD”
end
end

calculator $ rspec
F
Failures:
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

--

--

--

Web Developer, Marathon Runner, Coffee Drinker.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Designing Right Abstractions

Task description:

The Function that you can count on: Enumerate

Getting started building Augmented Reality apps in Xamarin

Build your own operating system #5_integrate_segmentation

Cheatsheet; Free up Linux disk space

Negotiation and top down programming in your coding interview

Grab the Grapes, Part 2

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
Andy

Andy

Web Developer, Marathon Runner, Coffee Drinker.

More from Medium

Why to start with testing your code

Beginner’s guide to creating a command-line application in Ruby

A plant sprouting from a jar of money

Transpiler VS Compiler

App Basics with ActiveRecord