调用未定义的函数tests \ codeception \ unit \ models \ expect()

时间:2016-10-07 06:49:27

标签: yii2 phpunit codeception

我正在尝试使用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 &#039;yii\base\ErrorException&#039; with message &#
039;Call to undefined function tests\codeception\unit\models\expect()&#039;

in E:\xampp\htdocs\news\tests\codeception\unit\models\ContactFormTest.php:46

Stack trace:
#0 [internal function]: yii\base\ErrorHandler-&gt;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

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您的IDE是否识别expect内的specify函数调用?

如果没有,请尝试将specifyverify添加到手册中指定的require-dev Composer依赖项中:

"require-dev": {
    ...
    "codeception/specify": "*",
    "codeception/verify": "*"
}

并运行composer update

P.S。对于讲俄语的读者来说link with credit