applying rspec best practises

17
Applying RSpec Best Practices Kevin McKelvin Rubyfuza 2013

Upload: neil-henegan

Post on 19-Jan-2015

466 views

Category:

Technology


2 download

DESCRIPTION

Kevin McKelvin Rubyfuza 2013

TRANSCRIPT

Page 1: Applying RSpec Best Practises

Applying RSpec Best Practices

Kevin McKelvinRubyfuza 2013

Page 2: Applying RSpec Best Practises

Ground Rules

Tests must support OO principles

Tests must be technical documentationfor the code

••

Page 3: Applying RSpec Best Practises

The ExampleConway’s Game of Life

DeterministicZero Player Game

Page 4: Applying RSpec Best Practises

Be descriptive

it “should work!” do

end

Page 5: Applying RSpec Best Practises

Describe a Ruby Constant

The describe block

describe Game do

end

Page 6: Applying RSpec Best Practises

The describe block

describe Game do describe "#tick" do

end

Prefix instance methods with #and class methods with .

end

Page 7: Applying RSpec Best Practises

The context blockdescribe Game do describe "#tick" do

end

Use contexts to find edge cases

end

context "when given an empty world" do

end

Start contexts with when or with

Page 8: Applying RSpec Best Practises

The it blockdescribe Game do describe "#tick" do

end

“Do or do not, there is no try” -Yoda

end

context "when given an empty world" do

endend

it "returns an empty world" do

Page 9: Applying RSpec Best Practises

Use RSpec Matchersdescribe Game do describe "#tick" do

endend

context "when given an empty world" do

endend

it "returns an empty world" do

empty_world = World.new second_world = subject.tick( empty_world)

second_world.empty?.must.equal true

Page 10: Applying RSpec Best Practises

Use RSpec Matchersdescribe Game do describe "#tick" do

endend

context "when given an empty world" do

endend

it "returns an empty world" do

empty_world = World.new second_world = subject.tick( empty_world)

second_world.must.be_empty

Page 11: Applying RSpec Best Practises

RSpec Settings

--color --profile --format d

.rspec

Page 12: Applying RSpec Best Practises

Running Your Specs

Game #tick when given an empty world returns an empty world when given a world with one cell returns an empty world when given a world with three adjacentcells returns a world with one cell

Page 13: Applying RSpec Best Practises

Remember TDD

Support Object Oriented best-practices

SOLIDLaw of Demeter

Page 14: Applying RSpec Best Practises

Remember TDD

Don’t test your framework

Page 15: Applying RSpec Best Practises

Remember TDD

Avoid integration testing where possible

Page 16: Applying RSpec Best Practises

Remember TDD

Be the first consumer of your public API

Page 17: Applying RSpec Best Practises

ResourcesRSpec: http://rspec.info/The RSpec Book: http://pragprog.com/book/achbd/the-rspec-bookBetter Specs:http://www.betterspecs.orgMustard: https://github.com/ryanb/mustard