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

Testando Software para Dormir Melhor

Testando Software para Dormir Melhor

Vitor Capela

July 11, 2017
Tweet

More Decks by Vitor Capela

Other Decks in Programming

Transcript

  1. TCO

  2. TDD

  3. class DoSomethingImportant def do_it user = User.find_by(email: user_email) product =

    Product.find(product_id) availability = backend.check_availability(product) if availability.any_available? reservation = backend.reserve_for_purchase(product) if points_program.eligible?(user) backend.pay_with_points(reservation, points_program.authorize(user, allocated_points)) else backend.pay_with_credit_card(reservation, credit_card_information) end else fail NoProductsAvailableError end end end
  4. class DoSomethingImportant def do_it user = User.find_by(email: user_email) product =

    Product.find(product_id) availability = backend.check_availability(product) if availability.any_available? reservation = backend.reserve_for_purchase(product) if points_program.eligible?(user) backend.pay_with_points(reservation, points_program.authorize(user, allocated_points)) else backend.pay_with_credit_card(reservation, credit_card_information) end else fail NoProductsAvailableError end end end
  5. class DoSomethingImportant def do_it user = User.find_by(email: user_email) product =

    Product.find(product_id) availability = backend.check_availability(product) if availability.any_available? reservation = backend.reserve_for_purchase(product) if points_program.eligible?(user) backend.pay_with_points(reservation, points_program.authorize(user, allocated_points)) else backend.pay_with_credit_card(reservation, credit_card_information) end else fail NoProductsAvailableError end end end
  6. class OrdersController < ApplicationController def create @order = Order.new(order_params) if

    order.save fulfillment = Backend.new.fulfill_order(order) if fulfillment OrderMailer.order_confirmation(order.customer).deliver_n ow redirect_to confirmation_order_path(order), notice: 'Yes!' else OrderMailer.order_failed(order.customer).deliver_now redirect_to confirmation_order_path(order), alert: 'No!' end else flash[:alert] = 'Oh, oh! No order for you!' render :new end end end
  7. class FooBarBaz def initialize(foo, bar, baz) @foo = foo @bar

    = bar @baz = baz end def change_things_everywhere if @foo do_foo elsif @bar do_bar elsif @baz do_baz end end # !!... end
  8. class Shape def rotate(deg) fail 'Child, do this' end end

    class Square < Shape def rotate(deg) # !!... end end class Circle < Shape def rotate(deg) self end end class RotationTool def rotate(shape, deg) shape.rotate(deg) end end
  9. class Shape def rotate(deg) fail 'Child, do this' end end

    class Square < Shape def rotate(deg) # !!... end end class Circle < Shape def rotate(deg) self end end class RotationTool def rotate(shape, deg) shape.rotate(deg) end end
  10. class CreateOrder def initialize(product_id, params) @product_id = product_id @params =

    params end attr_reader :product_id, :params def call product = Product.find(product_id) order = Backend.new.create_order(product, params) # !!... end end
  11. class CreateOrder def initialize(product_id, params, product_repository, backend) @product_id = product_id

    @params = params @product_repository = product_repository @backend = backend end attr_reader :backend, :product_repository, :product_id, :params def call product = product_repository.find(product_id) order = backend.create_order(product, params) # !!... end end
  12. class CreateOrder def initialize(backend = Backend.new) @backend = backend end

    attr_reader :backend def call(product, params) order = backend.create_order(product, params) # !!... end end
  13. RSpec.describe CreateOrder do let(:backend) { double('Backend') } subject { described_class.new(backend)

    } describe '#call' do context 'when the Backend cannot be reached' do before do expect(backend) .to receive(:create_order) .and_raise(Errno!::ENETUNREACH) end it 'wraps the error' do expect { subject.call(nil, nil) } .to raise_error(OrderCreationError) end end end end
  14. RSpec.describe CreateOrder do subject { described_class.new(backend) } describe '#call' do

    context 'when the Backend cannot be reached' do let(:backend) do Class.new def create_order(*) fail Errno!::ENETUNREACH end end.new end it 'wraps the error' do expect { subject.call(nil, nil) } .to raise_error(OrderCreationError) end end end end