我按照本教程: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);
}
}
好吧,我等待解決方案! Muchas Gracias! – Sangar82 2013-04-04 19:48:58
嘿@ Sangar82代碼已經更新。抱歉的混亂! – 2013-04-04 19:57:27
你已經刪除了$ badCode-> expect($ this-> once()) - >方法('checkPassword') - > with($ password);但爲什麼?這似乎應該工作,但我不明白,因爲在刪除這些行之前測試失敗 – Sangar82 2013-04-04 20:08:23