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

6d5b10739d6cc9b5667575c6f24056a5?s=128

Lucas Marinho

July 06, 2013
Tweet

Transcript

  1. RSpec Love @lmarinho

  2. None
  3. RSpec Love @lmarinho

  4. None
  5. RSpec Love @lmarinho

  6. RSpec Love @lmarinho

  7. RSpec Love @lmarinho

  8. Code Spec

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

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

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

  15. Use expect .should class Proxy < BasicObject def initialize(target) @target

    = target end def method_missing(*args, &block) @target.__send__(*args, &block) end ... end
  16. None
  17. Use describe Referencia estrutura do código testado

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

    describe ‘.class_method’ do ...
  19. None
  20. Use context Define um estado ou entrada no sistema

  21. Use context context ‘when the user has no username’ do

    ... context ‘when the account balance is 0.0’ do ...
  22. None
  23. Contexto Expectativa

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

  25. Use let let(:email) { ‘email@example.com’ }

  26. 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
  27. Use let Prefira ‘let’ a ‘before’, pois o primeiro é

    lazy-loaded
  28. None
  29. Use subject Subject ajuda a evitar repetição de referências

  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
  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
  32. None
  33. Use “skim its” Skim milk < 0.3% gordura Skim it

    1 expectation & < 4 LOC
  34. None
  35. Crie métodos Métodos ajudam a evitar repetições desnecessárias

  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') }
  37. Crie métodos Métodos também ajudam a evitar loops em specs

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

  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
  41. Reuse specs describe Array do it_behaves_like ‘a collection’ end describe

    Set do it_behaves_like ‘a collection’ end
  42. None
  43. Use factories As vezes...

  44. Use factories Pois FactoryGirl é legal!

  45. None
  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
  47. 0

  48. None
  49. Perguntas?

  50. Referências betterspecs.org e links associados