BDD com Rails
http://www.slideshare.net/caueguerra




    caue.guerra@caelum.com.br
OndeTrabalhar.com
Corretoras

             Comecei trabalhando
             com corretoras

             Planilhas que operavam
             diretamente na BOVESPA
Quero uma planilha que faça todo o
                           trabalho por mim.
                               Pra ontem!



Trader



         É pra já




                                     Recurso
Usuário pede
                        funcionalidade




Testes eram feitos em
produção.


                           Coding




                           Deploy
Acmus
public WaveformDisplay(Composite parent, int style) {

    
     super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL);


    
    _parent = parent;


    
    _hbar = getHorizontalBar();

    
    _hbar.setMinimum(0);

    
    _hbar.addSelectionListener(new SelectionListener() {

    
    
    public void widgetDefaultSelected(SelectionEvent se) {

    
    
    
     widgetSelected(se);

    
    
    }


    
    
    public void widgetSelected(SelectionEvent se) {


     
     
          
          
               
               
                     updateXStart();
                     for (Waveform w : _wf) {
                                                                         Vou escrever testes, mas o

    
    
    
     
    w.redraw();                                   código está muito acoplado.


     
     
          
          
               
               
                     }
                     _timeBar.redraw();                                      Para isso precisarei

    
    
    }                                                          refatorar, mas não tenho

    
    });

    
    _hbar.setVisible(false);                                      segurança para refatorar se

    
    _vbar = getVerticalBar();
                                                                            eu não tenho testes.

    
    _vbar.setMinimum(0);

    
    _vbar.addSelectionListener(new SelectionListener() {

    
    
    public void widgetDefaultSelected(SelectionEvent se) {

    
    
    
     widgetSelected(se);

    
    
    }


    
    
    public void widgetSelected(SelectionEvent se) {

    
    
    
     updateYStart();

    
    
    
     for (Waveform w : _wf) {

    
    
    
     
    w.redraw();


     
     
          
          
               
               }
                     }
                                                                 Codigo muito feio não dava


     
     
          });
          _vbar.setVisible(false);                                       segurança

    
    _wf = new ArrayList<Waveform>();                            E se eu quebrar?

    
    _sampleArrays = new ArrayList<SampleArray>();


    
    setBackground(AcmusGraphics.WHITE);                     E ainda multiplicamos por 5


     
     
          GridLayout gl = new GridLayout();
          gl.marginHeight = 0;
                                                                   a cobertura. de 0.3% para


     
     
          gl.marginWidth = 0;
          gl.horizontalSpacing = 0;
                                                                           1.5%

    
    gl.verticalSpacing = 1;

    
    setLayout(gl);


    
    _timeBar = new TimeBar(this);

    
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

    
    gridData.heightHint = _xBarHeight;

    
    _timeBar.setLayoutData(gridData);


    
    amplitude();

    }
public WaveformDisplay(Composite parent, int style) {

    
     super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL);


    
    _parent = parent;


    
    _hbar = getHorizontalBar();

    
    _hbar.setMinimum(0);

    
    _hbar.addSelectionListener(new SelectionListener() {

    
    
    public void widgetDefaultSelected(SelectionEvent se) {

    
    
    
     widgetSelected(se);

    
    
    }


    
    
    public void widgetSelected(SelectionEvent se) {


     
     
          
          
               
               
                     updateXStart();
                     for (Waveform w : _wf) {
                                                                         Vou escrever testes, mas o

    
    
    
     
    w.redraw();                                   código está muito acoplado.


     
     
          
          
               
               
                     }
                     _timeBar.redraw();                                      Para isso precisarei

    
    
    }                                                          refatorar, mas não tenho

    
    });

    
    _hbar.setVisible(false);                                      segurança para refatorar se

    
    _vbar = getVerticalBar();
                                                                            eu não tenho testes.

    
    _vbar.setMinimum(0);

    
    _vbar.addSelectionListener(new SelectionListener() {

    
    
    public void widgetDefaultSelected(SelectionEvent se) {

    
    
    
     widgetSelected(se);

    
    
    }


    
    
    public void widgetSelected(SelectionEvent se) {

    
    
    
     updateYStart();

    
    
    
     for (Waveform w : _wf) {

    
    
    
     
    w.redraw();


     
     
          
          
               
               }
                     }
                                                                 Codigo muito feio não dava


     
     
          });
          _vbar.setVisible(false);                                       segurança

    
    _wf = new ArrayList<Waveform>();                            E se eu quebrar?

    
    _sampleArrays = new ArrayList<SampleArray>();


    
    setBackground(AcmusGraphics.WHITE);                     E ainda multiplicamos por 5


     
     
          GridLayout gl = new GridLayout();
          gl.marginHeight = 0;
                                                                   a cobertura. de 0.3% para


     
     
          gl.marginWidth = 0;
          gl.horizontalSpacing = 0;
                                                                           1.5%

    
    gl.verticalSpacing = 1;

    
    setLayout(gl);


    
    _timeBar = new TimeBar(this);

    
    GridData gridData = new GridData(GridData.FILL_HORIZONTAL);

    
    gridData.heightHint = _xBarHeight;

    
    _timeBar.setLayoutData(gridData);


    
    amplitude();

    }
@Test
public void testSimulateWallShocking() {
   vectors = new ArrayList<Vector>();

 vectors.add(new Vector(0.457495710997814f,
0.457495710997814f, 0.7624928516630234f));


 RayTracingSimulation rts = new
RayTracingSimulation(sectors, vectors, soundSourceCenter,
sphericalReceptorCenter, sphericalReceptorRadius,
soundSpeed, initialEnergy, mCoeficient, k);

 

 rts.simulate(bar);

 

 Iterator<Double> itr =
rts.getSphericalReceptorHistogram().keySet().iterator();


 Double expected = 0.02739239;

 assertTrue(Math.abs(expected - itr.next()) < EPS);

}
class TestaCalculadora
  def test_soma
   Calculadora.soma 1
   Calculadora.soma 2

  if Calculadora.total == 3
    puts "teste passando"
  else
    puts "teste falhou"
  end
 end
end
TestUnit
class TestaCalculadora          class TestaCalculadora < Test::Unit::TestCase
   def test_soma
     Calculadora.soma 1
                                  def test_soma
     Calculadora.soma 2             Calculadora.soma 1
                                    Calculadora.soma 2
    if Calculadora.total == 3
      puts "teste passando"
    else                            assert_equal 3, Calculadora.total
      puts "teste falhou"         end
    end
  end                           end
end
RSpec

                                                it "should be equal 3 when add 1 and 2" do
class TestaCalculadora < Test::Unit::TestCase
   def test_soma                                   Calculadora.soma 1
     Calculadora.soma 1                            Calculadora.soma 2
     Calculadora.soma 2

    assert_equal 3, Calculadora.total             Calculadora.total.should equal 3
  end
end                                             end
it "should be equal 3 when add 1 and 2" do   Scenario: Summing 1 and 2
    Calculadora.soma 1                         Given a calculator
    Calculadora.soma 2
                                               When I add 1
  Calculadora.total.should equal 3             And I add 2
end
                                               Then the result should be 3
it "should be equal 3 when add 1 and 2" do   Cenário: Somando 1 e 2
    Calculadora.soma 1                        Dado uma calculadora
    Calculadora.soma 2
                                              Quando eu somo 1
  Calculadora.total.should equal 3            E eu somo 2
end
                                              Então o resultado deve ser 3
OndeTrabalhar.com
   95.7% de cobertura - rcov
TDD / BDD ?
Teste




  Código




Refatoração
Vamos codar!
@Test
public void addAPrioritizableCard() {
	 given.thereIsAnUserNamed("sergio").and()
	 	 	 .thereIsAProjectNamed("IEs4Linux")
   	 	 	 .ownedBy("sergio").and()
	 	 	 .iAmLoggedInAs("sergio");
	 when.iOpenProjectPageOf("IEs4Linux").and()
	 	    .iOpenCardsPage().and()
   	   .iAddTheCard("support IE8")
	 	 	 	 .prioritizable()
	 	 	 	 .withDescription("Micro$oft must be supported");
	 then.theCard("support IE8").isPrioritizable();
}
E você?

Que desculpa ainda usa
  para não testar?
Produtividade ?
Digitação não é o gargalo!

Quanto tempo gasta
consultando API?
Perguntando pra colegas
duvidas? etc




 O que determina a
 produtividade hoje,
  é a qualidade de
       ontem
http://groups.google.com/group/dojo_sp/
Test all the fucking time
Think all the fucking time
Meu argumento final
Obrigado!

BDD com Rails

  • 1.
  • 2.
    http://www.slideshare.net/caueguerra caue.guerra@caelum.com.br
  • 3.
  • 4.
    Corretoras Comecei trabalhando com corretoras Planilhas que operavam diretamente na BOVESPA
  • 5.
    Quero uma planilhaque faça todo o trabalho por mim. Pra ontem! Trader É pra já Recurso
  • 6.
    Usuário pede funcionalidade Testes eram feitos em produção. Coding Deploy
  • 10.
  • 11.
    public WaveformDisplay(Composite parent,int style) { super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL); _parent = parent; _hbar = getHorizontalBar(); _hbar.setMinimum(0); _hbar.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent se) { widgetSelected(se); } public void widgetSelected(SelectionEvent se) { updateXStart(); for (Waveform w : _wf) { Vou escrever testes, mas o w.redraw(); código está muito acoplado. } _timeBar.redraw(); Para isso precisarei } refatorar, mas não tenho }); _hbar.setVisible(false); segurança para refatorar se _vbar = getVerticalBar(); eu não tenho testes. _vbar.setMinimum(0); _vbar.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent se) { widgetSelected(se); } public void widgetSelected(SelectionEvent se) { updateYStart(); for (Waveform w : _wf) { w.redraw(); } } Codigo muito feio não dava }); _vbar.setVisible(false); segurança _wf = new ArrayList<Waveform>(); E se eu quebrar? _sampleArrays = new ArrayList<SampleArray>(); setBackground(AcmusGraphics.WHITE); E ainda multiplicamos por 5 GridLayout gl = new GridLayout(); gl.marginHeight = 0; a cobertura. de 0.3% para gl.marginWidth = 0; gl.horizontalSpacing = 0; 1.5% gl.verticalSpacing = 1; setLayout(gl); _timeBar = new TimeBar(this); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.heightHint = _xBarHeight; _timeBar.setLayoutData(gridData); amplitude(); }
  • 12.
    public WaveformDisplay(Composite parent,int style) { super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL); _parent = parent; _hbar = getHorizontalBar(); _hbar.setMinimum(0); _hbar.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent se) { widgetSelected(se); } public void widgetSelected(SelectionEvent se) { updateXStart(); for (Waveform w : _wf) { Vou escrever testes, mas o w.redraw(); código está muito acoplado. } _timeBar.redraw(); Para isso precisarei } refatorar, mas não tenho }); _hbar.setVisible(false); segurança para refatorar se _vbar = getVerticalBar(); eu não tenho testes. _vbar.setMinimum(0); _vbar.addSelectionListener(new SelectionListener() { public void widgetDefaultSelected(SelectionEvent se) { widgetSelected(se); } public void widgetSelected(SelectionEvent se) { updateYStart(); for (Waveform w : _wf) { w.redraw(); } } Codigo muito feio não dava }); _vbar.setVisible(false); segurança _wf = new ArrayList<Waveform>(); E se eu quebrar? _sampleArrays = new ArrayList<SampleArray>(); setBackground(AcmusGraphics.WHITE); E ainda multiplicamos por 5 GridLayout gl = new GridLayout(); gl.marginHeight = 0; a cobertura. de 0.3% para gl.marginWidth = 0; gl.horizontalSpacing = 0; 1.5% gl.verticalSpacing = 1; setLayout(gl); _timeBar = new TimeBar(this); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.heightHint = _xBarHeight; _timeBar.setLayoutData(gridData); amplitude(); }
  • 13.
    @Test public void testSimulateWallShocking(){ vectors = new ArrayList<Vector>(); vectors.add(new Vector(0.457495710997814f, 0.457495710997814f, 0.7624928516630234f)); RayTracingSimulation rts = new RayTracingSimulation(sectors, vectors, soundSourceCenter, sphericalReceptorCenter, sphericalReceptorRadius, soundSpeed, initialEnergy, mCoeficient, k); rts.simulate(bar); Iterator<Double> itr = rts.getSphericalReceptorHistogram().keySet().iterator(); Double expected = 0.02739239; assertTrue(Math.abs(expected - itr.next()) < EPS); }
  • 17.
    class TestaCalculadora def test_soma Calculadora.soma 1 Calculadora.soma 2 if Calculadora.total == 3 puts "teste passando" else puts "teste falhou" end end end
  • 18.
    TestUnit class TestaCalculadora class TestaCalculadora < Test::Unit::TestCase def test_soma Calculadora.soma 1 def test_soma Calculadora.soma 2 Calculadora.soma 1 Calculadora.soma 2 if Calculadora.total == 3 puts "teste passando" else assert_equal 3, Calculadora.total puts "teste falhou" end end end end end
  • 19.
    RSpec it "should be equal 3 when add 1 and 2" do class TestaCalculadora < Test::Unit::TestCase def test_soma Calculadora.soma 1 Calculadora.soma 1 Calculadora.soma 2 Calculadora.soma 2 assert_equal 3, Calculadora.total Calculadora.total.should equal 3 end end end
  • 20.
    it "should beequal 3 when add 1 and 2" do Scenario: Summing 1 and 2 Calculadora.soma 1 Given a calculator Calculadora.soma 2 When I add 1 Calculadora.total.should equal 3 And I add 2 end Then the result should be 3
  • 21.
    it "should beequal 3 when add 1 and 2" do Cenário: Somando 1 e 2 Calculadora.soma 1 Dado uma calculadora Calculadora.soma 2 Quando eu somo 1 Calculadora.total.should equal 3 E eu somo 2 end Então o resultado deve ser 3
  • 22.
    OndeTrabalhar.com 95.7% de cobertura - rcov
  • 23.
  • 24.
  • 26.
  • 28.
    @Test public void addAPrioritizableCard(){ given.thereIsAnUserNamed("sergio").and() .thereIsAProjectNamed("IEs4Linux") .ownedBy("sergio").and() .iAmLoggedInAs("sergio"); when.iOpenProjectPageOf("IEs4Linux").and() .iOpenCardsPage().and() .iAddTheCard("support IE8") .prioritizable() .withDescription("Micro$oft must be supported"); then.theCard("support IE8").isPrioritizable(); }
  • 34.
    E você? Que desculpaainda usa para não testar?
  • 37.
  • 38.
    Digitação não éo gargalo! Quanto tempo gasta consultando API? Perguntando pra colegas duvidas? etc O que determina a produtividade hoje, é a qualidade de ontem
  • 39.
  • 40.
    Test all thefucking time
  • 41.
    Think all thefucking time
  • 42.
  • 44.