Skip to content

Instantly share code, notes, and snippets.

@isidromerayo
Created January 24, 2012 18:06
Show Gist options
  • Save isidromerayo/1671582 to your computer and use it in GitHub Desktop.
Save isidromerayo/1671582 to your computer and use it in GitHub Desktop.
Second test with Mockito - Point Of Sale - Hola TDD
@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€");
}
<?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');
}
<?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€');
}
<?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€');
}
<?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');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment