How to catch PHP Warning in PHPUnit

后端 未结 6 1048
迷失自我
迷失自我 2021-01-01 15:47

I am writing test cases and here is a question I have.

So say I am testing a simple function someClass::loadValue($value)

The normal test case

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-01 16:18

    Make SomeClass throw an error when input is invalid and tell phpUnit to expect an error.

    One method is this:

    class ExceptionTest extends PHPUnit_Framework_TestCase
    {
        public function testLoadValueWithNull()
        {
            $o = new SomeClass();            
            $this->setExpectedException('InvalidArgumentException');
            $this->assertInstanceOf('InvalidArgumentException', $o::loadValue(null));
        }
    }
    

    See documentation for more methods.

提交回复
热议问题