Install rspec, rspec-core, rspec-expectations, rspec-mocks, rspec-support
$ gem install rspec
Get help
$ rspec --help
- Describe the methods of the class you are testing
- Initialize a new object
- Call methods on the object
- The 'expect' clause tests what we expect to happen
require "rspec"
RSpec.describe Car do
describe "#new" do
it "creates a new instance when passed make, model, and year" do
car = Car.new("Tesla", "Model S", 2014)
expect(car).to be_a(Car)
end
it "initializes the odometer to zero" do
car = Car.new("Toyota", "Prius", 2010)
expect(car.odometer).to eq(0)
end
end
end
- Write a test case, and run it. It should fail. (Red)
- Write only enough code to make the test pass. (Green)
- Cleanup code, extract methods, eliminate duplication. (Refactor)
- Repeat
echo "--color --format doc" > .rspec
- Circle class
- Pig Latin
- Blackjack
- initialize with a radius and optional position
- instance methods:
- radius:
r
- diameter:
d = 2r
- area:
a = πr^2
- point within circle:
(x1 - x)^2 + (y1 - y)^2 <= r^2
- radius:
- class methods:
- total area
- initialize with a string
- instance methods:
- translate
- rules:
- starts with consonant, move to end, append 'ay'
- starts with vowel, append 'way'
- classes:
- Card
- Deck
- Hand