php单测 异常测试

2020-09-19  本文已影响0人  浩克啊12138

在写单测的时候忘记怎么测试异常了现在就记录一下异常测试的写法

    /**
     * @expectedException  \Exception
     * @expectedExceptionMessage   货币代码错误!
     */
    public function testCreateCurrency()
    {
        $currency = $this->generateCurrency();
        $result = $this->getFundsRechargeService()->createCurrency($currency);
        $this->assertEquals(1, $result);

        $this->getFundsRechargeService()->createCurrency($currency);
    }

以上为一般写法, 但是开始测试的时候发现现在的phpunit开始不支持这种注释写法了, 如图1


图1
    public function testCreateCurrency()
    {
        $this->expectExceptionMessage('货币代码错误!');
        $currency = $this->generateCurrency();
        $result = $this->getFundsRechargeService()->createCurrency($currency);
        $this->assertEquals(1, $result);

        $this->getFundsRechargeService()->createCurrency($currency);
    }

现在可以使用的为通过方法去捕获以上了

上一篇 下一篇

猜你喜欢

热点阅读