Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
RSpec Love
Lucas Marinho
July 06, 2013
Programming
0
150
RSpec Love
Compartilho 10 dicas para melhorar os seus Specs. Referência: betterspecs.org
Lucas Marinho
July 06, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
You CANt teach an old dog new tricks
michaelbukachi
0
110
확장 가능한 테라폼 코드 관리 (Scalable Terraform Code Management)
posquit0
1
320
TechFeed Conference 2022 - Kotlin Experimental
jmatsu
0
780
Better Reliability through Observability (and Experimentation)
ksatirli
PRO
1
250
Milestoner
bkuhlmann
1
200
Reinventing the wheel ... as a service
mariofusco
3
270
Reactでアプリケーションを構築する多様化
sakito
4
3.4k
もしも、 上司に鬼退治を命じられたら~プロジェクト計画編~
higuuu
0
280
microCMS × Shopifyで、ECサイトがリニューアル後急成長した話
microcms
0
470
質とスピード(2022春版、質疑応答用資料付き) / Quality and Speed 2022 Spring Edition
twada
PRO
28
18k
Named Document って何?
harunakano
0
380
SRE NEXT 2022: Sensible Incident Management for Software Startups
takanabe
2
340
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
21
5.4k
How GitHub Uses GitHub to Build GitHub
holman
465
280k
WebSockets: Embracing the real-time Web
robhawkes
57
5k
Web Components: a chance to create the future
zenorocha
303
40k
Side Projects
sachag
449
37k
Reflections from 52 weeks, 52 projects
jeffersonlam
337
17k
The Language of Interfaces
destraynor
148
20k
GitHub's CSS Performance
jonrohan
1020
410k
The Pragmatic Product Professional
lauravandoore
19
2.9k
What's in a price? How to price your products and services
michaelherold
229
9.3k
We Have a Design System, Now What?
morganepeng
35
2.9k
Fashionably flexible responsive web design (full day workshop)
malarkey
396
62k
Transcript
RSpec Love @lmarinho
None
RSpec Love @lmarinho
None
RSpec Love @lmarinho
RSpec Love @lmarinho
RSpec Love @lmarinho
Code Spec
None
None
DISCLAIMER: É SÓ A MINHA OPINIÃO
None
Use expect foo.should eq(bar) foo.should_not eq(bar)
Use expect expect(foo).to eq(bar) expect(foo).not_to eq(bar)
Use expect .should class Proxy < BasicObject def initialize(target) @target
= target end def method_missing(*args, &block) @target.__send__(*args, &block) end ... end
None
Use describe Referencia estrutura do código testado
Use describe describe MyClass do ... describe ‘#method’ do ...
describe ‘.class_method’ do ...
None
Use context Define um estado ou entrada no sistema
Use context context ‘when the user has no username’ do
... context ‘when the account balance is 0.0’ do ...
None
Contexto Expectativa
Use let Let ajuda a definir nomes relacionados a contextos
Use let let(:email) { ‘email@example.com’ }
Use let let(:email) { ‘email@example.com’ } let(:user) { User.new(email: email)
} context ‘when there is no email’ do let(:email) { ‘’ } it ‘is invalid’ do expect(user).to be_invalid end end
Use let Prefira ‘let’ a ‘before’, pois o primeiro é
lazy-loaded
None
Use subject Subject ajuda a evitar repetição de referências
Use subject describe User do it ‘has the expected attributes’
do user = User.new expect(user).to respond_to :name expect(user).to respond_to :email expect(user).to respond_to :age end end
Use subject describe User do subject { User.new } it
{ should respond_to :name } it { should respond_to :email } it { should respond_to :age } end
None
Use “skim its” Skim milk < 0.3% gordura Skim it
1 expectation & < 4 LOC
None
Crie métodos Métodos ajudam a evitar repetições desnecessárias
Crie métodos it { should_create_report_named('Volume Evolution') } it { should_create_report_named('Top
Users') } it { should_create_report_named('Top Terms') } it { should_create_report_named('Gender Report') }
Crie métodos Métodos também ajudam a evitar loops em specs
None
Reuse specs É possível reusar specs através de shared_examples
Reuse specs shared_examples ‘a collection’ do let(:collection) { described_class.new([7, 2,
4]) } context ‘when initialized with 3 items’ do it ‘says it has three items’ do expect(collection.size).to eq(3) end end end
Reuse specs describe Array do it_behaves_like ‘a collection’ end describe
Set do it_behaves_like ‘a collection’ end
None
Use factories As vezes...
Use factories Pois FactoryGirl é legal!
None
Use mocks? Mock-based tests are more coupled to the interfaces
in your system, while classical tests are more coupled to the implementation of an object’s collaborators. -- Myron Marston
0
None
Perguntas?
Referências betterspecs.org e links associados