Php/phpunit/モックオブジェクト/テスト対象がconstructを含まない場合
提供: 初心者エンジニアの簡易メモ
2015年5月20日 (水) 03:13時点における127.0.0.1 (トーク)による版 (ページの作成:「constructを実行せずに、テストしたい場合のサンプル ==サンプル== *application/point class Point { private $_point = 0; public function...」)
constructを実行せずに、テストしたい場合のサンプル
サンプル
- application/point
class Point
{
private $_point = 0;
public function __construct($point)
{
$this->_point = $point;
}
public function add($point)
{
$this->_point += $point;
return $this;
}
public function getPoint()
{
return $this->_point;
}
}
- test/application/PointMockConstructTest.php
require_once __DIR__ . '/../../application/Point.php';
class PointMockConstructTest extends PHPUnit_Framework_TestCase
{
public function testGetPoint()
{
// Point クラスのスタブを作成します (テスト対象クラスにconstructがあっても無視します。
$stub = $this->getMockBuilder('Point')
->disableOriginalConstructor()
->getMock();
// スタブの設定を行います
$stub->expects($this->any())
->method('getPoint')
->will($this->returnValue(10));
// $stub->getPoint() をコールすると 10 を返すようになります
$this->assertThat($stub->getPoint(), $this->equalTo(10));
}
}
