Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RSpec Love

RSpec Love

Compartilho 10 dicas para melhorar os seus Specs. Referência: betterspecs.org

Lucas Marinho

July 06, 2013
Tweet

Other Decks in Programming

Transcript

  1. RSpec Love
    @lmarinho

    View Slide

  2. View Slide

  3. RSpec Love
    @lmarinho

    View Slide

  4. View Slide

  5. RSpec Love
    @lmarinho

    View Slide

  6. RSpec Love
    @lmarinho

    View Slide

  7. RSpec Love
    @lmarinho

    View Slide

  8. Code Spec

    View Slide

  9. View Slide

  10. View Slide

  11. DISCLAIMER:
    É SÓ A MINHA OPINIÃO

    View Slide

  12. View Slide

  13. Use expect
    foo.should eq(bar)
    foo.should_not eq(bar)

    View Slide

  14. Use expect
    expect(foo).to eq(bar)
    expect(foo).not_to eq(bar)

    View Slide

  15. Use expect
    .should
    class Proxy < BasicObject
    def initialize(target)
    @target = target
    end
    def method_missing(*args, &block)
    @target.__send__(*args, &block)
    end
    ...
    end

    View Slide

  16. View Slide

  17. Use describe
    Referencia estrutura do
    código testado

    View Slide

  18. Use describe
    describe MyClass do ...
    describe ‘#method’ do ...
    describe ‘.class_method’ do ...

    View Slide

  19. View Slide

  20. Use context
    Define um estado ou
    entrada no sistema

    View Slide

  21. Use context
    context ‘when the user has no
    username’ do ...
    context ‘when the account
    balance is 0.0’ do ...

    View Slide

  22. View Slide

  23. Contexto
    Expectativa

    View Slide

  24. Use let
    Let ajuda a definir nomes
    relacionados a contextos

    View Slide

  25. Use let
    let(:email) { ‘[email protected]’ }

    View Slide

  26. Use let
    let(:email) { ‘[email protected]’ }
    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

    View Slide

  27. Use let
    Prefira ‘let’ a ‘before’, pois o
    primeiro é lazy-loaded

    View Slide

  28. View Slide

  29. Use subject
    Subject ajuda a evitar
    repetição de referências

    View Slide

  30. 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

    View Slide

  31. Use subject
    describe User do
    subject { User.new }
    it { should respond_to :name }
    it { should respond_to :email }
    it { should respond_to :age }
    end

    View Slide

  32. View Slide

  33. Use “skim its”
    Skim milk < 0.3% gordura
    Skim it
    1 expectation
    &
    < 4 LOC

    View Slide

  34. View Slide

  35. Crie métodos
    Métodos ajudam a evitar
    repetições desnecessárias

    View Slide

  36. 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') }

    View Slide

  37. Crie métodos
    Métodos também ajudam a
    evitar loops em specs

    View Slide

  38. View Slide

  39. Reuse specs
    É possível reusar specs
    através de shared_examples

    View Slide

  40. 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

    View Slide

  41. Reuse specs
    describe Array do
    it_behaves_like ‘a collection’
    end
    describe Set do
    it_behaves_like ‘a collection’
    end

    View Slide

  42. View Slide

  43. Use factories
    As vezes...

    View Slide

  44. Use factories
    Pois FactoryGirl é legal!

    View Slide

  45. View Slide

  46. 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

    View Slide

  47. 0

    View Slide

  48. View Slide

  49. Perguntas?

    View Slide

  50. Referências
    betterspecs.org e links
    associados

    View Slide