假设你有一种最终归结为的方法
class Pager { private $i; public function next() { if ($this->i >= 3) { throw new OutOfBoundsException(); } $this->i++; } }
你将如何对这门课程进行单元测试.即测试是否在使用PHPUnit的next()的第三次调用中抛出异常?我添加了我的尝试作为答案,但我不确定这是否真的是要走的路.
如何在前两个调用上测试null并测试抛出的异常,如下所示:
class PagerTest { public function setUp() { $this->pager = new Pager(); } public function testTooManyNextCalls() { $this->assertNull($this->pager->next()); $this->assertNull($this->pager->next()); $this->assertNull($this->pager->next()); $this->setExpectedException('OutOfBoundsException'); $this->pager->next(); } }