dongsilu2237 2013-04-04 17:05
浏览 143
已采纳

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

I`m following this tutorial: http://jtreminio.com/2013/03/unit-testing-tutorial-part-5-mock-methods-and-overriding-constructors/ . A great tutorial for learn how works PHPUnit.

But I´m not able to understand because the test not pass.

The failure is:

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

At this part of code:

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

But this is not possible because the next soft assertion runs inside checkPassword method and pass the test.

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

It fails because is a mock method and the behaviour is different? Or the code is wrong?

I attach all the files for easy understanding, it is a small example.

Console

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

Someone can help me? Thanks!

Update

The author of the blog updated the tutorial with the solution. Can't do any assertions against mock methods, only stubs.

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条回答 默认 最新

  • doumi7854 2013-04-04 19:37
    关注

    author here - I fudged that part up, and AppFog (my host) is having issues with free accounts (mine) so I can't update it right now.

    Also, yes it should be $this->user in checkPassword()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?