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

Test anti-patterns

Test anti-patterns

Lightning talk presented in a small event at Aracaju - Sergipe

Rafael França

February 15, 2014
Tweet

More Decks by Rafael França

Other Decks in Technology

Transcript

  1. The test reader is not able to see the cause

    and effect between fixture and verification logic because part of it is done outside the Test Method.
  2. • A fixture • An instance variable defined far way

    from the test • A variable without a good name Sym ptom s
  3. describe User do it 'can be a developer' do user

    = users(:rafael) expect(user).to be_developer end end Sym ptom s
  4. describe User do let(:developer) { users(:developer) } ! context 'when

    it has GitHub account' do it 'bla bla bla' do ... end ! it 'bla bla bla' do ... end ! it 'bla bla bla' do ... end ! it 'is a developer' do expect(developer).to be_developer end end end Sym ptom s
  5. describe User do context 'user account exists with a GitHub

    id' do before do @user = User.create(name: 'Foo', active: true, gh_user_id: 123456) end end end Sym ptom s
  6. describe User do it 'can be a developer' do user

    = create(:user, role: 'developer') expect(user).to be_developer # assert_equal true, user.developer? end end Solution
  7. describe User do context 'when it has GitHub account' do

    it 'bla bla bla' do ... end ! it 'bla bla bla' do ... end ! it 'bla bla bla' do ... end ! it 'is a developer' do developer = users(:developer) expect(developer).to be_developer end end end Solution
  8. describe User do context 'user account exists with a GitHub

    id' do before do @user = create(:user, gh_user_id: 123456) end end end Solution
  9. whatever class, object or method we are testing; when we

    are writing customer tests, the SUT is probably the entire application or at least a major subsystem of it.
  10. Sym ptom s describe User do it 'gets the GitHub

    information' do user_information = ... expect(User).to( receive(:get_information).and_return(user_information) ) ! expect(User.find_by_id(user_uid)).to( eq(User.new(user_information)) ) end end
  11. Solution describe User do it 'gets the GitHub information' do

    user_information = ... client = double('GitHub client') ! expect(client).to receive(:get).and_return(user_information) ! expect(User.find_by_id(client, user_uid)).to( eq(User.new(user_information)) ) end end
  12. • Loss of confidence • Take longer to add new

    features • Decrease of Refactoring and code improvements Im pact
  13. • Talk with your team • Address the root causes

    • Not enough time • Hard to test code • Wrong test automation strategy • Write regression tests Solution