通过phpunit检查传递的参数的数据类型

时间:2017-01-19 07:51:59

标签: arguments phpunit instance

使用PHPUnit我想检查传递给我的类构造函数的参数是否具有正确的数据类型并抛出异常,但我不知道如何实现它。

说我有这个示例类:

Class Foo {
    public function __construct(Bar $bar) {
        if (!($bar instanceof Bar)) {
            throw new InvalidArgumentException();
        }
    }
}

测试方法:

/**
 * @expectedException InvalidArgumentException
 */
public function test_it_has_the_correct_data_type() {
    $foo = new Foo('bar') // having this line will result to must be an instance of Bar error message
}

0 个答案:

没有答案