laravel 未知的格式化程序

oewdyzsn  于 2023-01-10  发布在  其他
关注(0)|答案(4)|浏览(146)

我已经尝试了所有可能的方法,但是我无法弄清我做错了什么。我试图用虚拟数据加载我的数据库,但是我一直得到未知的格式化程序"描述"。描述是我正在使用的变量之一。
下面是我的工厂代码和我的播种机编码器

use Faker\Generator as Faker;
use Analytics\Blockgrant;

$factory->define(Blockgrant::class, function (Faker $faker) {
    return [
        'description' => $faker->description,
        'value' => $faker->value
    ];
});
<?php

use Faker\Generator as Faker;
use Universityobfanalytics\Blockgrantcomponents;

$factory->define(Blockgrantcomponents::class, function (Faker $faker) {
    return [
        'blockgrants_id' => $faker->blockgrants_id,
        'description' => $faker->description,
        'percentage' => $faker->percentage,
        'value' => $faker->value
    ];
});
<?php

use Illuminate\Database\Seeder;
use Analytics\Blockgrant;
use Analytics\Blockgrantcomponents;

class BlockgrantSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(Blockgrant::class, 10)->create()->each(function ($blockgrant) {
            $blockgrant->blockgrantcomponents()->save(factory(Blockgrantcomponents::class)->create());

        });
    }
}

我使用的是一对一的hasOnebelongsTo关系
有人能帮忙告诉我哪里做错了吗?

ac1kyiln

ac1kyiln1#

这可能是因为您在测试中使用的是PHPUnit\Framework\TestCase而不是Tests\TestCase

lymnna71

lymnna712#

faker库没有您试图访问的属性。
您只能使用如下格式化程序:

$faker->name
$faker->text
$faker->paragraphs() 
$faker->sentences()

您最好浏览faker文档,查看可用格式化程序here的完整列表

6ss1mwsb

6ss1mwsb3#

我正在使用Pest,当尝试在我的项目中编写单元测试时,我的解决方案也出现了同样的错误:
tests/Pest.php文件中,我有这样一行:

uses(Tests\TestCase::class)->in('Feature');

我把它改成了这个,它起作用了

uses(Tests\TestCase::class)->in('Feature', 'Unit');

在类Test/TestCase中,记住在构造函数中调用parent::setup(),如下所示:

protected function setUp(): void
{
    try {
        parent::setUp();
    } catch (QueryException $e) {
        file_put_contents(
            getcwd() . '/testing_env_log',
            json_encode([
                'db' => Config::get('database'),
                'env' => $_ENV,
                'app' => Config::get('app'),
            ], JSON_THROW_ON_ERROR)
        );

        throw $e;
    }
    Event::fake();
}
4ngedf3f

4ngedf3f4#

您需要创建自己的TestCase类,该类将使用您创建的CreatesApplication特征。
所有这一切看起来像这样:

class TestCase extends BaseTestCase
{
    use CreatesApplication;
}
trait CreatesApplication
{
    public function createApplication(): Application
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

相关问题