【问题标题】:Soft assertion and mock methods test failure软断言和模拟方法测试失败
【发布时间】:2013-04-04 17:05:59
【问题描述】:

我正在关注本教程: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);
    }
}

【问题讨论】:

    标签: php unit-testing testing phpunit


    【解决方案1】:

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

    另外,是的,它应该是 checkPassword() 中的 $this-&gt;user

    【讨论】:

    • 好的,我等待解决方案!穆哈斯·格拉西亚斯!
    • 嘿@Sangar82 代码已更新。为混乱道歉!
    • 你已经删除了$badCode->expects($this->once())->method('checkPassword')->with($password);但为什么?看来这应该可行,但我不明白,因为在删除这些行之前测试失败
    • @Sangar82 我犯了一个错误,忘记了你不能对模拟方法做任何断言,只有存根。我认为在模拟中允许一些-&gt;expects() 会非常有帮助。
    • 感谢您的回复,希望您关注单元测试教程。干得好!
    【解决方案2】:

    您在setMethods 中缺少一个方法:

    ->setMethods(array('callExit'))
    

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

    【讨论】:

    • 我也这么认为(而且可能是正确的),但是这个例子仍然行不通
    • 这个例子是为了学习mock和stub方法的区别。在示例中, checkPassword 不是 int setMethods 因为是 Mock Method 而 callExit 是 Stub Method
    • @Sangar82 是的,你是对的。我已经学会了...感谢您的教程。现在我理解了你的代码,它看起来还可以。您需要 callExit() 作为存根,否则它将结束测试套件(使用退出)。将需要更深入的测试。您使用的是哪个 PHPUnit 版本?但是你应该有一件事(虽然)它不直接属于问题:在checkPassword()中使用$this-&gt;user而不是$user
    • @hek2mgl 我正在使用 3.7.18。类名是 BadCode,对这个例子没有太多期望,但很好地解释了存根方法和模拟方法之间的区别。如果删除这些行,则测试通过。似乎都正确,但没有通过测试,这让我很感兴趣
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多