调用未定义函数 tests\codeception\unit\models\expect()
Call to undefined function tests\codeception\unit\models\expect()
我正在尝试使用 yii2 基本应用程序中的默认 ContactFormTest 执行单元测试。但它为我提供了以下错误。
E:\xampp\htdocs\news\tests>codecept run unit codeception\unit\models\ContactForm
Test.php
Codeception PHP Testing Framework v2.0.16
Powered by PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
←[1mUnit Tests (1) ←[22m--------------------------------------------------------
------------------------------------
←[35;1mTest contact←[39;22m (tests\codeception\unit\models\ContactFormTest::test
Contact)<pre>PHP Fatal Error 'yii\base\ErrorException' with message &#
039;Call to undefined function tests\codeception\unit\models\expect()'
in E:\xampp\htdocs\news\tests\codeception\unit\models\ContactFormTest.php:46
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}</pre>
下面是我的ContactFormTest.php
namespace tests\codeception\unit\models;
use app\models\ContactForm;
use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
use Codeception\Util\Debug;
class ContactFormTest extends TestCase
{
use Specify;
protected function setUp()
{
parent::setUp();
Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
return 'testing_message.eml';
};
}
protected function tearDown()
{
unlink($this->getMessageFile());
parent::tearDown();
}
public function testContact()
{
/** @var ContactForm $model */
$model = $this->getMockBuilder('app\models\ContactForm')
->setMethods(['validate'])
->getMock();
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
$model->attributes = [
'name' => 'Tester',
'email' => 'tester@example.com',
'subject' => 'very important letter subject',
'body' => 'body of current message',
];
//Debug::debug($model->contact('admin@example.com'));
//die();
$this->specify('email should be send', function () use ($model) {
expect('ContactForm::contact() should return true', $model->contact('admin@example.com'))->true(); //this throws the error
expect('email file should exist', file_exists($this->getMessageFile()))->true();
});
$this->specify('message should contain correct data', function () use ($model) {
$emailMessage = file_get_contents($this->getMessageFile());
expect('email should contain user name', $emailMessage)->contains($model->name);
expect('email should contain sender email', $emailMessage)->contains($model->email);
expect('email should contain subject', $emailMessage)->contains($model->subject);
expect('email should contain body', $emailMessage)->contains($model->body);
});
}
private function getMessageFile()
{
return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
}
}
和unit.suite.yml
# Codeception Test Suite Configuration
# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: UnitTester
modules:
config:
Db:
dsn: 'mysql:host=127.0.0.1;dbname=news_tests'
user: 'root'
password: ''
dump: 'tests/_data/dump.sql'
populate: false
cleanup: false
reconnect: true
如何解决这个问题?
您的 IDE 是否识别 specify
中的 expect
函数调用?
如果没有,则尝试将 specify and verify 添加到 require-dev
手册中指定的 Composer 依赖项:
"require-dev": {
...
"codeception/specify": "*",
"codeception/verify": "*"
}
和运行 composer update
.
P.S。 link with credit 面向俄语读者。
我正在尝试使用 yii2 基本应用程序中的默认 ContactFormTest 执行单元测试。但它为我提供了以下错误。
E:\xampp\htdocs\news\tests>codecept run unit codeception\unit\models\ContactForm
Test.php
Codeception PHP Testing Framework v2.0.16
Powered by PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
←[1mUnit Tests (1) ←[22m--------------------------------------------------------
------------------------------------
←[35;1mTest contact←[39;22m (tests\codeception\unit\models\ContactFormTest::test
Contact)<pre>PHP Fatal Error 'yii\base\ErrorException' with message &#
039;Call to undefined function tests\codeception\unit\models\expect()'
in E:\xampp\htdocs\news\tests\codeception\unit\models\ContactFormTest.php:46
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleFatalError()
#1 {main}</pre>
下面是我的ContactFormTest.php
namespace tests\codeception\unit\models;
use app\models\ContactForm;
use Yii;
use yii\codeception\TestCase;
use Codeception\Specify;
use Codeception\Util\Debug;
class ContactFormTest extends TestCase
{
use Specify;
protected function setUp()
{
parent::setUp();
Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
return 'testing_message.eml';
};
}
protected function tearDown()
{
unlink($this->getMessageFile());
parent::tearDown();
}
public function testContact()
{
/** @var ContactForm $model */
$model = $this->getMockBuilder('app\models\ContactForm')
->setMethods(['validate'])
->getMock();
$model->expects($this->once())->method('validate')->will($this->returnValue(true));
$model->attributes = [
'name' => 'Tester',
'email' => 'tester@example.com',
'subject' => 'very important letter subject',
'body' => 'body of current message',
];
//Debug::debug($model->contact('admin@example.com'));
//die();
$this->specify('email should be send', function () use ($model) {
expect('ContactForm::contact() should return true', $model->contact('admin@example.com'))->true(); //this throws the error
expect('email file should exist', file_exists($this->getMessageFile()))->true();
});
$this->specify('message should contain correct data', function () use ($model) {
$emailMessage = file_get_contents($this->getMessageFile());
expect('email should contain user name', $emailMessage)->contains($model->name);
expect('email should contain sender email', $emailMessage)->contains($model->email);
expect('email should contain subject', $emailMessage)->contains($model->subject);
expect('email should contain body', $emailMessage)->contains($model->body);
});
}
private function getMessageFile()
{
return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
}
}
和unit.suite.yml
# Codeception Test Suite Configuration
# suite for unit (internal) tests.
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: UnitTester
modules:
config:
Db:
dsn: 'mysql:host=127.0.0.1;dbname=news_tests'
user: 'root'
password: ''
dump: 'tests/_data/dump.sql'
populate: false
cleanup: false
reconnect: true
如何解决这个问题?
您的 IDE 是否识别 specify
中的 expect
函数调用?
如果没有,则尝试将 specify and verify 添加到 require-dev
手册中指定的 Composer 依赖项:
"require-dev": {
...
"codeception/specify": "*",
"codeception/verify": "*"
}
和运行 composer update
.
P.S。 link with credit 面向俄语读者。