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

标签 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);
    }
}

有人可以帮助我吗?谢谢!

更新

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

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);
    }
}

最佳答案

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

此外,是的,它应该是 checkPassword() 中的 $this->user

关于php - 软断言和模拟方法测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817394/

相关文章:

c# - Moq 断言一个抽象方法被调用

java - 在不同计算机/路径上使用的 Selenium Webdrivers

javascript - Jasmine spy 没有找到属性(property)

php - Ajax 请求转到新页面而不是更新当前页面?

php - 在一个选项中从数据库发布多个值

PHP自动包含

php - 按金额排序 Mysql 值列表

javascript - 如何对主要职责是重定向到应用程序外部页面的 Angular 服务进行单元测试?

ajax - 单元测试/集成测试 GXT 代码的最佳方法是什么?

javascript - 如何使用 Nightwatch.js 创建新的浏览器实例