【问题标题】:How to test cake3 plugins with fixtures如何使用夹具测试 cake3 插件
【发布时间】:2015-09-12 09:55:06
【问题描述】:

我在尝试使用固定装置测试插件时遇到了麻烦。

我在 tests/Fixture 下创建了fixtures文件,结构为文档:

namespace Files\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
 * FiledFixture
 *
 */
class FiledFixture extends TestFixture {

    /**
     * Table name
     *
     * @var string
     */
    public $table = 'filed';

    /**
     * Fields
     *
     * @var array
     */
    public $fields = [
        'id' => [
            'type' => 'integer',
            'length' => 11,
            ...
        ],
        'foreign_key' => [
            'type' => 'integer',
            ...
        ],
        ...
    ];

    /**
     * Records
     *
     * @var array
     */
    public $records = [
        [
            'id' => 1,
            'foreign_key' => 1,
            'file_id' => 1,
            'filedtype_id' => 1,
            'model' => 'Lorem ipsum dolor sit amet',
            'language' => 'Lore',
            'created' => '2014-11-01 19:21:54',
            'modified' => '2014-11-01 19:21:54'
        ],
    ];
}

然后在测试用例中,我用公共变量fixtures加载fixtures:

<?php
namespace Files\Test\TestCase\Model\Table;

use Cake\ORM\TableRegistry;
use Files\Model\Table\FiledTable;
use Cake\TestSuite\TestCase;

/**
 * Files\Model\Table\FiledTable Test Case
 */
class FiledTableTest extends TestCase {

    /**
     * Fixtures
     *
     * @var array
     */
    public $fixtures = [
        'plugin.files.filed',
        'plugin.files.files',
        'plugin.files.filedtypes',
        'plugin.files.thumbs'
    ];
...

当我尝试运行 phpunit 时,出现下一个错误:

..Exception: Referenced fixture class "Files\Test\Fixture\FiledFixture" not found. Fixture "plugin.files.filed" was referenced in test case "Files\Test\TestCase\Model\Table\FiledTableTest". in [/home/genar/src/metropolitan-web-2015/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 180]

我尝试创建一个自定义 bootstrap.php 进行测试,但我不知道必须有什么。我复制了这个https://github.com/Xety/Cake3-Upload/blob/master/tests/bootstrap.php,但它引发了很多错误。

我也用谷歌搜索过,但我找不到任何关于插件测试的信息,也找不到官方文档。

关于如何实现这一点的任何想法?

【问题讨论】:

  • tests/fixtures,真的吗?或者这只是您问题中的一个错误,因为默认情况下,灯具必须进入test/Fixture
  • 实际上是测试/夹具,正如官方文档 book.cakephp.org/3.0/en/development/… 中所解释的那样。这是我的问题的一个错误,我的文件夹是正确的,并且无法加载 Fixtures...

标签: php cakephp testing cakephp-3.0


【解决方案1】:

好吧,最后我认为错误是在生成 composer 自动加载器的方式上。我一直在查看我的 Test\Fixture\ 命名空间如何没有添加到 autoload_psr4.php 上,或者运行 composer dump-autoload。 所以我解决这个问题的方法是在插件的composer.json 上添加正确的行并运行composer update 以很好地生成自动加载。

注意composer dump-autoload 在供应商下安装插件时不会更新您的插件 psr4 路由。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多