Phpunit,期望一个方法正好运行两次

标签 php unit-testing phpunit

class TestMe
{
    public function method() { }
}

测试:

class TestTest extends PHPUnit_Framework_TestCase
{
    public function testA()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
    }

    public function testB()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
        $stub->method();
    }

    public function testC()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
        $stub->method();
        $stub->method();
    }

    public function testD()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
        $stub->method();
        $stub->method();
        $stub->method();
    }
}

testA、testB、testC 通过,testD 仅失败,这很奇怪。 testA 甚至没有调用该方法,所以它应该失败了——但它通过了,为什么? testB 调用该方法一次,但我们预期调用两次,所以它应该失败了——但它通过了,为什么? testC没问题,没问题 testD 失败所以没问题

也许 exactly() 并没有像我预期的那样工作。我正在使用最新的 4.3.4 PhPunit。

最佳答案

尝试在 getMock 调用中添加您要模拟的方法名称。

为了获得方面的结果,我将测试类修改为:

class TestTest extends \PHPUnit_Framework_TestCase
{
    public function testA()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method');
    }

    public function testB()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method')->withAnyParameters();
        $stub->method();
    }

    public function testC()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method')->withAnyParameters();
        $stub->method();
        $stub->method();
    }

    public function testD()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method')->withAnyParameters();
        $stub->method();
        $stub->method();
        $stub->method();
    }
}

结果是:

PHPUnit 4.3.4 by Sebastian Bergmann.

There were 3 failures:

1) Acme\DemoBundle\Tests\TestTest::testA
Expectation failed for method name is equal to <string:method> when invoked 2 time(s).
Method was expected to be called 2 times, actually called 0 times.

2) Acme\DemoBundle\Tests\TestTest::testB
Expectation failed for method name is equal to <string:method> when invoked 2 time(s).
Method was expected to be called 2 times, actually called 1 times.

3) Acme\DemoBundle\Tests\TestTest::testD
TestMe::method() was not expected to be called more than 2 times.

希望对你有帮助

关于Phpunit,期望一个方法正好运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26672977/

相关文章:

php - 如何配置PHPunit测试结果输出到HTML文件?

exception - 在 zend 框架中运行 Controller 单元测试时出现 "No default module defined for this application"异常

php - 如果 woocommerce 订单包含特定产品类别的商品,则发送电子邮件

PHP:使用 Zend_Barcode 调整条形码的输出大小 | Magento 发票条形码

java - 如何模拟 javax.servlet.ServletInputStream

python - 单元测试 Flask 应用程序时的模板

javascript - 在 Karma/Jasmine 单元测试中注入(inject) Angular 的 $timeout 服务的实际实现

php - 为什么 PHPUnit 测试在机器与互联网断开连接时运行得更快?

PHP 帮助设置数据库内容的样式

php - 尝试清除 Symfony 4 应用程序缓存时出现 "Cannot autowire service"异常