I am trying to perform unit testing using the default ContactFormTest inside the yii2 basic application. But it provides me the following error.
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>
Below is my 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' => '[email protected]',
'subject' => 'very important letter subject',
'body' => 'body of current message',
];
//Debug::debug($model->contact('[email protected]'));
//die();
$this->specify('email should be send', function () use ($model) {
expect('ContactForm::contact() should return true', $model->contact('[email protected]'))->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';
}
}
And 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
How to fix this?
Does your IDE recognize the expect
function calls inside specify
?
If not, then try adding specify and verify to require-dev
Composer dependencies as specified in the manual:
"require-dev": {
...
"codeception/specify": "*",
"codeception/verify": "*"
}
And run composer update
.
P.S. link with credit for Russian-speaking readers.