Slide 1

Slide 1 text

@igorflorianfs IGOR FLORIAN

Slide 2

Slide 2 text

CONCEITOS QUE PODEM TE AJUDAR A ESCREVER TESTES MELHORES

Slide 3

Slide 3 text

Quando conheci Ruby …

Slide 4

Slide 4 text

TESTAR É IMPORTANTE

Slide 5

Slide 5 text

refatoração one-line test OMG ❤ ❤ ❤ let & subject everywhere DRY this, DRY that shared examples!!!!! OMFG rubylindus2s2s2

Slide 6

Slide 6 text

NO INICIO, ENCONTREI ALGUNS CONTEÚDOS

Slide 7

Slide 7 text

RSpec Factory Girl Capybara Cucumber RSpec RSpec RSpec RSpec RSpec RSpec RSpec RSpec RSpec RSpec RSpec

Slide 8

Slide 8 text

#PARTIUTESTAR

Slide 9

Slide 9 text

Na prática…

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

TESTAR É DIFICIL

Slide 12

Slide 12 text

Conhecer apenas não é o suficiente para testar bem

Slide 13

Slide 13 text

Precisamos de conceitos

Slide 14

Slide 14 text

AS IDEIAS DE UM BOM TESTE ERAM FRÁGEIS

Slide 15

Slide 15 text

MANUTENÇÃO SOFRÍVEL

Slide 16

Slide 16 text

E o maior problema era a legibilidade

Slide 17

Slide 17 text

Onde cê quer chegar? +

Slide 18

Slide 18 text

Quero apresentar conceitos e palavras-chave

Slide 19

Slide 19 text

- ESTRUTURA - TESTES OBSCUROS - FERRAMENTAS - DRY - TESTES AUTOCONTIDOS

Slide 20

Slide 20 text

Mas antes… ✋

Slide 21

Slide 21 text

OBJETIVOS ALÉM DE COBERTURA E GARANTIA

Slide 22

Slide 22 text

TESTE NÃO SERVE APENAS PARA COBERTURA

Slide 23

Slide 23 text

DOCUMENTAÇÃO?

Slide 24

Slide 24 text

Como exemplo com certeza!

Slide 25

Slide 25 text

EXEMPLOS PRECISAM DE EXPRESSIVIDADE

Slide 26

Slide 26 text

DÊ UMA BOA DESCRIÇÃO

Slide 27

Slide 27 text

Explicar o que o código do teste faz

Slide 28

Slide 28 text

it 'inactivates an order only 5 days after creation' do #... end it 'does not create an order with inactive customer' do #... end it 'creates a seller with unique cnpj' do #... end

Slide 29

Slide 29 text

É inviável ler todo o código de cada teste para entendê-los

Slide 30

Slide 30 text

PENSE NO FEEDBACK

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Descrição de teste é como se fosse o título de uma dissertação

Slide 34

Slide 34 text

O teste deve refletir a descrição

Slide 35

Slide 35 text

Ou vai tirar 0 na aula de dissertação

Slide 36

Slide 36 text

it 'creates a seller with unique cnpj' do customer = Customer.create( name: 'Customer', cpf: '999.999.999-99' ) expect(customer).to be_valid end

Slide 37

Slide 37 text

✅ it 'creates a seller with unique cnpj' do seller = Seller.create( name: 'Seller', cnpj: '99.999.999-99' ) expect(seller).to be_valid end

Slide 38

Slide 38 text

SUT (System Under Test)

Slide 39

Slide 39 text

describe Order do #… end

Slide 40

Slide 40 text

class OrderTest < ActiveSupport::TestCase #… end

Slide 41

Slide 41 text

COLABORADORES

Slide 42

Slide 42 text

it 'does not create an order with inactive customer' do # Collaborators customer = Customer.create!(name: 'John', status: :inactive) seller = Seller.create!(name: 'JewelStore') product = Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) # SUT order = Order.create!(customer: customer, seller: seller, product: product) end

Slide 43

Slide 43 text

4 ETAPAS DO TESTE
 (Estrutura canônica)

Slide 44

Slide 44 text

it 'does not pay an inactive order' do customer = Customer.create!(name: 'John') seller = Seller.create!(name: 'JewelStore') product = Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) credit_card = PaymentMethod.create!(name: 'Visa', type: 'credit card') order = Order.create!( customer: customer, seller: seller, product: product, status: :inactive ) payment = order.pay!(credit_card) expect(payment).to be(false) order.destroy! product.destroy! credit_card.destroy! seller.destroy! customer.destroy! end

Slide 45

Slide 45 text

ESTRUTURA TEXTUAL

Slide 46

Slide 46 text

NOVELAS

Slide 47

Slide 47 text

SETUP 
 (Preparação)

Slide 48

Slide 48 text

it 'does not pay an inactive order' do # Setup customer = Customer.create!(name: 'John') seller = Seller.create!(name: 'JewelStore') product = Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) credit_card = PaymentMethod.create!(name: 'Visa', type: 'credit card') order = Order.create!( customer: customer, seller: seller, product: product, status: :inactive ) # … end

Slide 49

Slide 49 text

EXERCSE
 (Exercício)

Slide 50

Slide 50 text

Execução do código da aplicação

Slide 51

Slide 51 text

it 'does not pay an inactive order' do # Setup #… # Exercise payment = order.pay!(credit_card) # … end

Slide 52

Slide 52 text

VERIFY ✅ 
 (Verificação)

Slide 53

Slide 53 text

it 'does not pay an inactive order' do # Setup #… # Exercise #… # Verify expect(payment).to be false #… end

Slide 54

Slide 54 text

TEARDOWN 
 (Desconstrução)

Slide 55

Slide 55 text

it 'does not pay an inactive order' do # … # Teardown order.destroy! product.destroy! credit_card.destroy! seller.destroy! customer.destroy! end

Slide 56

Slide 56 text

it 'does not pay an inactive order' do # Setup customer = Customer.create!(name: 'John') seller = Seller.create!(name: 'JewelStore') product = Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) credit_card = PaymentMethod.create!(name: 'Visa', type: 'credit card') order = Order.create!( customer: customer, seller: seller, product: product, status: :inactive ) # Exercise payment = order.pay!(credit_card) # Verify expect(payment).to be(false) # Teardown order.destroy! product.destroy! credit_card.destroy! seller.destroy! customer.destroy! end

Slide 57

Slide 57 text

TESTES OBSCUROS

Slide 58

Slide 58 text

MYSTERY GUEST

Slide 59

Slide 59 text

it 'pays an active order with credit card' do # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) end

Slide 60

Slide 60 text

subject(:order) { Order.create!(customer: customer, seller: seller, product: product, status: :active) } describe '#imaginary_method' do it 'some imaginary test 1' do # … end it 'some imaginary test 2' do # … end it 'some imaginary test 3' do # … end it 'some imaginary test 4' do # … end end describe '#pay!' do it 'pays an active order with credit card' do # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) end end context 'validations' do it 'does not create an order with inactive customer' do # … end it 'inactive an order only 5 days after creation' do # … end end

Slide 61

Slide 61 text

Se é relevante pro teste, pra que extrair?

Slide 62

Slide 62 text

✅ it 'pays an active order with credit card' do # Setup order = Order.create!( customer: customer, seller: seller, product: product, status: :active ) # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) end

Slide 63

Slide 63 text

EAGER TEST
 (IRRELEVANT INFORMATION)

Slide 64

Slide 64 text

CONVIDADO que deveria ser COADJUNVANTE

Slide 65

Slide 65 text

MUITA INFORMAÇÃO CAUSA DISTRAÇÃO

Slide 66

Slide 66 text

describe '#pay!' do it 'pays an active order with credit card' do # Setup customer = Customer.create!(name: 'John') seller = Seller.create!(name: 'JewelStore') product = Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) credit_card = PaymentMethod.create!(name: 'Visa', type: 'credit card') order = Order.create!(customer: customer, seller: seller, product: product, status: :active) # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) # Teardown # … end end

Slide 67

Slide 67 text

✅ let(:customer) { Customer.create!(name: 'John') } let(:seller) { Seller.create!(name: 'JewelStore') } let(:product) { Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) } let(:credit_card) { PaymentMethod.create!(name: 'Visa', type: 'credit card') } describe '#pay!' do it 'pays an active order with credit card' do # Setup order = Order.create!(customer: customer, seller: seller, product: product, status: :active) # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) # Teardown # … end end

Slide 68

Slide 68 text

Diga mais sobre o SUT e menos sobre os colaboradores

Slide 69

Slide 69 text

FERRAMENTAS

Slide 70

Slide 70 text

As features do RSpec são instigantes
 ❤

Slide 71

Slide 71 text

Problemas para soluções

Slide 72

Slide 72 text

MINITEST ROOTS

Slide 73

Slide 73 text

#FOCONOTESTE

Slide 74

Slide 74 text

É POSSÍVEL ADICIONAR COMPLEXIDADE ⚠

Slide 75

Slide 75 text

Com o tempo aprendi a viver sem RSpec

Slide 76

Slide 76 text

E quando voltei &, aprendi a usar suas features

Slide 77

Slide 77 text

describe Order do #… subject(:order) do Order.create!( customer: customer, seller: seller, product: product, status: :active ) end context 'with inactive customer' do let(:customer) { Customer.create!(name: 'Stiven', status: :inactive) } it 'does not pay the order' do # Exercise order.pay!(credit_card) # Verify expect(order).not_to be_paid end end end

Slide 78

Slide 78 text

✅ context 'active order payment' do subject(:order) { Order.create!(customer: customer, seller: seller, product: product, status: :active) } describe '#pay!' do it 'with credit card' do # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) end it 'with payment slip' do # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) end it 'with eletronic transfer' do #… end end end

Slide 79

Slide 79 text

✅ let(:network_error) { "You can't upload picture.jpg, network isn't available" } let(:encoding_error) { "The log.txt has an encoding error" } let(:errors) { network_error + " \n" + encoding_error } describe '#parse_by_type' do it 'parses all possible errors' do # Setup parser = Parser.new(errors) # Exercise parsed_errors = parser.parse_by_type # Verify expect(parsed_errors).to eql({ network: ['picture.jpg'], ecoding: ['log.txt'] }) end it 'parses network errors' do # Setup parser = Parser.new(network_error) # Exercise parsed_errors = parser.parse_by_type # Verify expect(parsed_errors).to eql({ network: ['picture.jpg'] }) end end

Slide 80

Slide 80 text

✅ let(:customer) { Customer.create!(name: 'John') } let(:seller) { Seller.create!(name: 'JewelStore') } let(:product) { Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) } let(:credit_card) { PaymentMethod.create!(name: 'Visa', type: 'credit card') } describe '#pay!' do it 'pays an active order with credit card' do # Setup order = Order.create!(customer: customer, seller: seller, product: product, status: :active) # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) # Teardown #… end end

Slide 81

Slide 81 text

DRY EM TESTES ☔

Slide 82

Slide 82 text

Escrever código TESTE != APLICAÇÃO

Slide 83

Slide 83 text

Os objetivos também

Slide 84

Slide 84 text

Técnicas de refatoração pragmáticas e tolerantes +

Slide 85

Slide 85 text

A maioria das situações não precisa de refatoração

Slide 86

Slide 86 text

“Escute seu código”, talvez a melhor saída seja repensar o design

Slide 87

Slide 87 text

"Pô, mas não tem jeito!" ✋

Slide 88

Slide 88 text

MÉTODOS DE TESTE E HELPERS

Slide 89

Slide 89 text

module AuthenticationHelper def login_as(user) session[:current_user_id] = user.id end end

Slide 90

Slide 90 text

before do login_as(users(:john)) end feature "product creation" do # ... end feature "product inactivation" do # ... end

Slide 91

Slide 91 text

describe UserImporter do # ... def generate_file(content) file = Tempfile.new('test_file') file.write(content) file end end

Slide 92

Slide 92 text

describe '#by_file' do it 'imports a male user' do # Setup file = generate_file('John | 23 | M') # Exercise user = UserImporter.by_file(file) # Verify expect(user.gender).to be_male # Teardown file.close! end it 'imports a female user' do # ... end end

Slide 93

Slide 93 text

Opte por clareza e objetividade

Slide 94

Slide 94 text

https://youtu.be/Is8ThG6Fetg

Slide 95

Slide 95 text

Pense no próximo
 
 ❤️

Slide 96

Slide 96 text

TESTES AUTOCONTIDOS

Slide 97

Slide 97 text

let(:customer) { Customer.create!(name: 'John') } let(:seller) { Seller.create!(name: 'JewelStore') } let(:product) { Product.create!(name: 'Ruby', seller: seller, price_unit: 2015) } let(:credit_card) { PaymentMethod.create!(name: 'Visa', type: 'credit card') } describe '#pay!' do it 'pays an active order with credit card' do # Setup order = Order.create!(customer: customer, seller: seller, product: product, status: :active) # Exercise order.pay!(credit_card) # Verify expect(order).to be_paid expect(order.payment_method).to eql(credit_card) # Teardown #… end end

Slide 98

Slide 98 text

OBRIGADO!

Slide 99

Slide 99 text

Referências

Slide 100

Slide 100 text

http://bit.ly/1kwh7Fo

Slide 101

Slide 101 text

http://bit.ly/1KtBaMD

Slide 102

Slide 102 text

http://bit.ly/1LFSrTO

Slide 103

Slide 103 text

http://bit.ly/1OmEVuv