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

Other Decks in Programming


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

    = target end def method_missing(*args, &block) @target.__send__(*args, &block) end ... end
  2. Use context context ‘when the user has no username’ do

    ... context ‘when the account balance is 0.0’ do ...
  3. 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
  4. 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
  5. Use subject describe User do subject { User.new } it

    { should respond_to :name } it { should respond_to :email } it { should respond_to :age } end
  6. 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') }
  7. 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
  8. 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
  9. 0