软断言和模拟方法测试失败

时间:2013-04-04 17:06:00

标签: php unit-testing testing phpunit

我正在学习本教程:http://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/。学习PHPUnit如何工作的好教程。

但我无法理解,因为测试没有通过。

失败的是:

Method was expected to be called 1 times, actually called 0 times.

在这段代码中:

        $badCode->expects($this->once())
            ->method('checkPassword')
            ->with($password);

但这是不可能的,因为下一个软断言在checkPassword方法中运行并通过测试。

        $badCode->expects($this->once())
            ->method('callExit');

它失败,因为是一个模拟方法,行为不同?或者代码错了?

我附上所有文件以便于理解,这是一个小例子。

控制台

PHPUnit 3.7.18 by Sebastian Bergmann.

FYOU SHALL NOT PASS............

Time: 0 seconds, Memory: 6.00Mb

There was 1 failure:

1) phpUnitTutorial\Test\BadCodeTest::testAuthorizeExitsWhenPasswordNotSet
Expectation failed for method name is equal to <string:checkPassword> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.


FAILURES!
Tests: 13, Assertions: 14, Failures: 1.

BadCode.php

<?php

namespace phpUnitTutorial;

class BadCode
{
    protected $user;

    public function __construct(array $user)
    {
        $this->user = $user;
    }

    public function authorize($password)
    {
        if ($this->checkPassword($password)) {
            return true;
        }

        return false;
    }

    protected function checkPassword($password)
    {
        if (empty($user['password']) || $user['password'] !== $password) {
            echo 'YOU SHALL NOT PASS';
            $this->callExit();
        }

        return true;
    }

    protected function callExit()
    {
        exit;
    }
}

BadCodeTest.php

<?php

namespace phpUnitTutorial\Test;

class BadCodeTest extends \PHPUnit_Framework_TestCase
{
    public function testAuthorizeExitsWhenPasswordNotSet()
    {
        $user = array('username' => 'jtreminio');
        $password = 'foo';

        $badCode = $this->getMockBuilder('phpUnitTutorial\BadCode')
            ->setConstructorArgs(array($user))
            ->setMethods(array('callExit'))
            ->getMock();

        $badCode->expects($this->once())
            ->method('checkPassword')
            ->with($password);

        $badCode->expects($this->once())
            ->method('callExit');

        $this->expectOutputString('YOU SHALL NOT PASS');

        $badCode->authorize($password);
    }
}
有人可以帮帮我吗?谢谢!

更新

博客作者使用该解决方案更新了教程。 不能对模拟方法做任何断言,只能对存根进行断言。

BadCode.php

<?php

namespace phpUnitTutorial;

class BadCode
{
    protected $user;

    public function __construct(array $user)
    {
        $this->user = $user;
    }

    public function authorize($password)
    {
        if ($this->checkPassword($password)) {
            return true;
        }

        return false;
    }

    protected function checkPassword($password)
    {
        if (empty($this->user['password']) || $this->user['password'] !== $password) {
            echo 'YOU SHALL NOT PASS';
            $this->callExit();
        }

        return true;
    }

    protected function callExit()
    {
        exit;
    }
}

BadCodeTest.php

<?php

namespace phpUnitTutorial\Test;

class BadCodeTest extends \PHPUnit_Framework_TestCase
{
    public function testAuthorizeExitsWhenPasswordNotSet()
    {
        $user = array('username' => 'jtreminio');
        $password = 'foo';

        $badCode = $this->getMockBuilder('phpUnitTutorial\BadCode')
            ->setConstructorArgs(array($user))
            ->setMethods(array('callExit'))

        $badCode->expects($this->once())
            ->method('callExit');

        $this->expectOutputString('YOU SHALL NOT PASS');

        $badCode->authorize($password);
    }
}

2 个答案:

答案 0 :(得分:0)

您错过了setMethods中的方法:

->setMethods(array('callExit'))

如果你想检查它是否被调用,你还需要添加checkPassword

答案 1 :(得分:0)

作者在这里 - 我捏造了那个部分,AppFog(我的主人)遇到了免费帐户问题(我的),所以我现在无法更新。

此外,是$this->user

中的checkPassword()