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

Conceitos que podem te ajudar a escrever testes...

Igor Florian
September 18, 2015

Conceitos que podem te ajudar a escrever testes melhores

#rubyconfbr 2015

Igor Florian

September 18, 2015
Tweet

More Decks by Igor Florian

Other Decks in Programming

Transcript

  1. refatoração one-line test OMG ❤ ❤ ❤ let & subject

    everywhere DRY this, DRY that shared examples!!!!! OMFG rubylindus2s2s2
  2. 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
  3. it 'creates a seller with unique cnpj' do customer =

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

    = Seller.create( name: 'Seller', cnpj: '99.999.999-99' ) expect(seller).to be_valid end
  5. 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
  6. 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
  7. 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
  8. it 'does not pay an inactive order' do # Setup

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

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

    # Teardown order.destroy! product.destroy! credit_card.destroy! seller.destroy! customer.destroy! end
  11. 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
  12. 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
  13. 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
  14. ✅ 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
  15. 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
  16. ✅ 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
  17. 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
  18. ✅ 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
  19. ✅ 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
  20. ✅ 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
  21. before do login_as(users(:john)) end feature "product creation" do # ...

    end feature "product inactivation" do # ... end
  22. 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
  23. 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