【问题标题】:Cakephp 3.0 unit testing errorCakephp 3.0 单元测试错误
【发布时间】:2015-06-12 00:04:17
【问题描述】:

我正在尝试在 CakePHP 3.0 中运行一些单元测试,但不断收到以下错误,这似乎是比我自己构建的更多 Cake 问题?有人有类似的问题吗?本文测试代码如下:http://book.cakephp.org/3.0/en/development/testing.html

namespace App\Test\TestCase\Controller;

use Cake\ORM\TableRegistry;
use Cake\TestSuite\IntegrationTestCase;

/**
 * App\Controller\UsersController Test Case
 */
class UsersControllerTest extends IntegrationTestCase
{

    /**
     * Test add method
     *
     * @return void
     */
    public function testAdd()
    {
        $data = [
            'username' => 'testusername',
            'password' => 'testpassword',
            'email' => 'testemail@test.ie',
            'location_id' => 5,
            'gender' => 'Male',
            'info' => 'test info'
        ];
        $this->post('/users', $data);

        $this->assertResponseSuccess();
        $users = TableRegistry::get('Users');
        $query = $users->find()->where(['username' => $data['username']]);
        $this->assertEquals(1, $query->count());
    }

回复:

1) App\Test\TestCase\Controller\UsersControllerTest::testIndex
Use of undefined constant TMP - assumed 'TMP'

C:\wamp\www\mysite\vendor\cakephp\cakephp\src\Network\Session.php:141
C:\wamp\www\mysite\vendor\cakephp\cakephp\src\Network\Session.php:95
C:\wamp\www\mysite\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCas
e.php:340
C:\wamp\www\mysite\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCas
e.php:267
C:\wamp\www\mysite\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCas
e.php:188

【问题讨论】:

  • 至少添加您的测试代码(您的测试代码可能有助于了解哪些常量TMP 未初始化)
  • 现在添加测试代码。

标签: php unit-testing cakephp phpunit cakephp-3.0


【解决方案1】:

您需要定义 CakePHP 工作所需的常量。这是在应用程序框架的 tests/bootstrap.php 文件中为您完成的:

https://github.com/cakephp/app/blob/master/tests/bootstrap.php#L8

通过包含应用程序引导程序,它还在此处创建定义常量:

https://github.com/cakephp/app/blob/master/config/paths.php#L61

【讨论】:

    【解决方案2】:

    如果有帮助,我也遇到了同样的错误,因为我的项目中缺少 phpunit.xml.dist 配置文件。

    您可以在这里找到它:https://github.com/cakephp/app/blob/master/phpunit.xml.dist

    【讨论】:

    • 您的链接我的过期时间请考虑回答问题。
    【解决方案3】:

    我遇到了同样的问题。这对我有用

    • 全局安装 PHPUnit
    • 使用 composer 创建 cakephp 项目使用 composer
    • 编写测试然后使用终端/CMD goto 你的项目的根 ex : c:/xampp/htdocs/someproject/
    • 然后输入命令

      c:/xampp/htdocs/someproject>phpunit

    它将运行所有测试。希望这对你有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 2022-08-19
      • 2014-10-24
      • 1970-01-01
      相关资源
      最近更新 更多