LoginSignup
1
0

More than 5 years have passed since last update.

PHPUnitで例外が投げられてるかを確認するテストの書き方

Last updated at Posted at 2015-12-18
    public function testDeleteFail() {
        // 例外が起きるようにモックを設定
        $BlogEntryMock = $this->getMockForModel('Blogs.BlogEntry', ['deleteAll']);
        $BlogEntryMock->expects($this->once())
            ->method('deleteAll')
            ->will($this->returnValue(false));

        // 例外のテスト
        $this->setExpectedException('InternalErrorException');
        $BlogEntryMock->deleteEntryByKey(1);
    }

setExpectedException()が廃止予定(2018/07/15追記)

参考 https://github.com/NetCommons3/NetCommons3/issues/1212

PHPUnit的には expectException() 使ってねってことみたいだけど、NetCommons3でも採用されてる CakePHP2だと同名のメソッドがあって、そっちも廃止予定のdeprecated…

代替え手段としては expectExceptionCode()expectExceptionMessage() あたりですかね。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0