public
Created

Second test with Mockito - Point Of Sale - Hola TDD

  • Download Gist
PointOfSale2Test.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
@Test
public void onBarcode_search_catalog() throws Exception {
Catalog catalog = mock(Catalog.class);
Screen screen = mock(Screen.class);
PointOfSale pointOfSale = new PointOfSale(catalog, screen);
pointOfSale.onBarcode("123");
verify(catalog).search("123");
}
@Test
public void onBarcode_show_price() throws Exception {
Catalog catalog = mock(Catalog.class);
Screen screen = mock(Screen.class);
when(catalog.search("123")).thenReturn("1€");
PointOfSale pointOfSale = new PointOfSale(catalog, screen);
pointOfSale.onBarcode("123");
verify(screen).show("1€");
}
PointOfSaleTest_Mockery.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<?php
 
/**
* @test
*/
public function onBarcode_search_catalog()
{
$catalog = m::mock('Catalog');
$screen = m::mock('Screen');
$screen->shouldReceive('show');
$pointOfSale = new PointOfSale($catalog,$screen);
// verify(catalog).search("123");
$catalog->shouldReceive('search')->once()->with('123');
$pointOfSale->onBarcode('123');
}
/**
* @test
*/
public function onBarcode_show_price()
{
$screen = m::mock('Screen');
$catalog = m::mock('Catalog');
// when(catalog.search("123").thenReturn("1€")
$catalog->shouldReceive('search')
->with('123')
->andReturn('1€');
$pointOfSale = new PointOfSale($catalog, $screen);
// verify(screen).show("1€");
$screen->shouldReceive('show')->with('1€')->once();
$pointOfSale->onBarcode('123');
}
PointOfSaleTest_PHPUnitMock.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php
 
/**
* @test
*/
public function onBarcode_search_catalog()
{
$catalog = $this->getMock('Catalog');
$screen = $this->getMock('Screen');
$pointOfSale = new PointOfSale($catalog, $screen);
// verify(catalog).search("123");
$catalog->expects($this->once())->method('search')->with('123');
$pointOfSale->onBarcode('123');
}
/**
* @test
*/
public function onBarcode_show_price() {
$catalog = $this->getMock('Catalog');
$screen = $this->getMock('Screen');
// when(catalog.search("123").thenReturn("1€")
$catalog->expects($this->once())
->method('search')
->with('123')
->will($this->returnValue('1€'));
$pointOfSale = new PointOfSale($catalog, $screen);
// verify(screen).show("1€");
$screen->expects($this->once())
->method('show')
->with('1€');
$pointOfSale->onBarcode('123');
}
PointOfSaleTest_Phake.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php
 
/**
* @test
*/
public function onBarcode_search_catalog()
{
$screen = Phake::mock('Screen');
$catalog = Phake::mock('Catalog');
$pointOfSale = new PointOfSale($catalog, $screen);
$pointOfSale->onBarcode('123');
// verify(catalog).search("123");
Phake::verify($catalog)->search('123');
}
/**
* @test
*/
public function onBarcode_show_price()
{
$screen = Phake::mock('Screen');
$catalog = Phake::mock('Catalog');
$pointOfSale = new PointOfSale($catalog, $screen);
// when(catalog.search("123").thenReturn("1€")
Phake::when($catalog)->search('123')->thenReturn('1€');
$pointOfSale->onBarcode('123');
// verify(screen).show("1€");
Phake::verify($screen)->show('1€');
}
PointOfSaleTest_Phockito.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php
 
/**
* @test
*/
public function onBarcode_search_catalog()
{
$screen = Phockito::mock('Screen');
$catalog = Phockito::mock('Catalog');
$pointOfSale = new PointOfSale($catalog, $screen);
$pointOfSale->onBarcode('123');
// verify(catalog).search("123");
Phockito::verify($catalog)->search('123');
}
/**
* @test
*/
public function onBarcode_show_price()
{
$screen = Phockito::mock('Screen');
$catalog = Phockito::mock('Catalog');
$pointOfSale = new PointOfSale($catalog , $screen);
// when(catalog.search("123").thenReturn("1€")
Phockito::when($catalog)->search('123')->return('1€');
$pointOfSale->onBarcode('123');
// verify(screen).show("1€");
Phockito::verify($screen)->show('1€');
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.