Getting started with RSpec — Part 2

Test Driven Development is the workflow of writing a test, letting it fail, and then changing your code to make it (and all the previous tests) pass.

# File: spec/calculator_spec.rbrequire './calculator.rb'describe "calculator" doit 'adds numbers' do
...
end
it 'subtracts numbers' do
calc = Calculator.new
expect(calc.subract(5,1)).to eql(4)
end
end
calculator $ bundle exec rspec
.F
Failures:1) calculator subtracts numbers
Failure/Error: expect(calc.subtract(5,1)).to eql(4)

NoMethodError:
undefined method `subtract' for #<Calculator:0x007f9dc4a65f40>
# ./spec/calculator_spec.rb:11:in `block (2 levels) in <top (required)>'
Finished in 0.00236 seconds (files took 0.08835 seconds to load)
2 examples, 1 failure
Failed examples:rspec ./spec/calculator_spec.rb:9 # calculator subtracts numbers
# File: calculator.rbclass Calculator
def add(x, y)
x + y
end

def subtract(x, y)
x - y
end
end
calculator $ rspec
..
Finished in 0.00224 seconds (files took 0.08993 seconds to load)
2 examples, 0 failures
# File: spec/calculator_spec.rbrequire './calculator.rb'describe 'calculator' do
it 'adds numbers' do
...
end
it 'subtracts numbers' do
...
end
it 'multiplies numbers' do
calc = Calculator.new
expect(calc.multiply(3,4)).to eql(12)
end
end
# File: calculator.rbclass Calculator
def add(x, y)
x + y
end
def subtract(x, y)
x - y
end
def multiply(x, y)
x*y
end
end
calculator $ rspec
...
Finished in 0.00262 seconds (files took 0.08638 seconds to load)
3 examples, 0 failures
# File: spec/calculator_spec.rbrequire './calculator.rb'describe 'calculator' do
it 'adds numbers' do
...
end
it 'subtracts numbers' do
...
end
it 'multiplies numbers' do
...
end
it 'divides numbers' do
calc = Calculator.new
expect(calc.divide(5,2)).to eql(2.5)
end
end
# File: calculator.rbclass Calculator
def add(x, y)
x + y
end
def subtract(x, y)
x - y
end
def multiply(x, y)
x*y
end
def divide(x, y)
x / y
end
end
calculator $ rspec
...F
Failures:1) calculator divides numbers
Failure/Error: expect(calc.divide(5,2)).to eql(2.5)

NoMethodError:
undefined method `divide' for #<Calculator:0x007fc9c0345440>
# ./spec/calculator_spec.rb:23:in `block (2 levels) in <top (required)>'
Finished in 0.00278 seconds (files took 0.09446 seconds to load)
4 examples, 1 failure
Failed examples:rspec ./spec/calculator_spec.rb:21 # calculator divides numbers
calculator $ rspec
...F
Failures:1) calculator divides numbers
Failure/Error: expect(calc.divide(5,2)).to eql(2.5)

expected: 2.5
got: 2

(compared using eql?)
# ./spec/calculator_spec.rb:23:in `block (2 levels) in <top (required)>'
Finished in 0.01352 seconds (files took 0.09081 seconds to load)
4 examples, 1 failure
Failed examples:rspec ./spec/calculator_spec.rb:21 # calculator divides numbers
def divide(x, y)
x / y.to_f
end
calculator $ rspec
....
Finished in 0.00271 seconds (files took 0.09715 seconds to load)
4 examples, 0 failures

--

--

--

Web Developer, Marathon Runner, Coffee Drinker.

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

Recommended from Medium

KAIDEX public Testnet 1.0 is here!

Java Strings

The Anemic Domain Model

Revisiting the Mathematical Foundations of Data Science

Young boy concentrating with pencil and paper.

Enterprise Open Source Governance and Scanning tool

SOLID — Single Responsibility Principle

Charles Web Debugging Proxy: How to use it and what is it for.

What are “Strings” in Python?

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

Moving from JavaScript to TypeScript

Magento Extensions Vs Magento Modules: What’s The Difference?

Magento Extensions and Modules

Crud Operation In React.js and Mysql

How to set up and use environment variables in your react app