CakePHPのテストで各種assertを試してみる
CakePHPのテストで各種assertを試してみる。
CakePHPのテストはPHPUnitを使ってるので、PHPUnitのメソッドがそのまま使えるみたい。
PHPUnit Manual http://phpunit.de/manual/3.7/ja/index.html
よく使いそうなassertを試してみる。
app/Test/Case/SampleTest.php
を下記のように編集する。
<?php class SampleTest extends CakeTestCase { /* * 基本的なassert */ public function testAssertNormal() { // 値が等しいか $this->assertEquals(100, 100); // 値が異なるか $this->assertNotEquals(100, 200); // 値が含まれるか $this->assertContains('bc', 'abcde'); } /* * 型関連のassert */ public function testAssertType() { // 型が一致するか $this->assertInternalType('int', 100); $this->assertInternalType('string', 'abcde'); $this->assertInternalType('array', array(100, 200, 300)); // インスタンスか $this->assertInstanceOf('Exception', new Exception); } /* * 配列関連のassert */ public function testAssertCount() { // 件数が一致するか $this->assertCount(3, array(100, 200, 300)); // キーが含まれるか $this->assertArrayHasKey(100, array(100 => 'abcde')); } /* * その他のassert */ public function testAssertEtc() { // trueかどうか $this->assertTrue(true); // falseかどうか $this->assertFalse(false); // nullかどうか $this->assertNull(null); // 空かどうか $this->assertEmpty(null); $this->assertEmpty(false); $this->assertEmpty(''); $this->assertEmpty(array()); } }