【问题标题】:PHPUnit test fails with InvalidArgumentException: Unknown formatter with Laravel 8 factoryPHPUnit 测试因 InvalidArgumentException 失败:Laravel 8 工厂的未知格式化程序
【发布时间】:2021-11-29 02:53:42
【问题描述】:

在我的 Laravel 8 项目中,我有这个动作类:

<?php

namespace App\Actions\Content;

use Illuminate\Support\Facades\Config;

class FixUriAction
{
    public function __invoke(string $uri)
    {
        if (preg_match('/^https?:\/\//i', $uri)) {
            return $uri;
        }

        return '/' . Config::get('current_lang')->code . '/' . $uri;
    }
}

我想为这个类编写单元测试,现在我的测试文件中有这段代码:

<?php

namespace Tests\Unit\Actions\Content;

use App\Actions\Content\FixUriAction;
use App\Models\Settings\Lang;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Config;
use PHPUnit\Framework\TestCase;

class FixUriActionTest extends TestCase
{
    use DatabaseTransactions;

    protected Lang $lang;
    protected FixUriAction $action;

    public function setUp(): void
    {
        parent::setUp();
        $this->action = new FixUriAction();
        $this->lang = Lang::factory()->make();
        Config::set('current_lang', $this->lang);
    }

    public function testShouldPrefixUriWithLangCode(): void
    {
        $uri = '/a-test-uri';
        $expectation = '/' . $this->lang->code . $uri;
        $result = ($this->action)($uri);

        $this->assertEquals($expectation, $result);
    }
}

在我的 LangFactory 我有这个代码:

<?php

namespace Database\Factories;

use App\Models\Settings\Lang;
use Illuminate\Database\Eloquent\Factories\Factory;

class LangFactory extends Factory
{
    protected $model = Lang::class;

    public function definition()
    {
        return [
            'name' => $this->faker->country,
            'code' => $this->faker->languageCode,
        ];
    }
}

当我运行phpunit tests/Unit/Actions/Content/FixUriActionTest.php 命令时,它会说:

There was 1 error:

1) Tests\Unit\Actions\Content\FixUriActionTest::testShouldPrefixUriWithLangCode
InvalidArgumentException: Unknown formatter "country"

我使用 PHPUnit 9.5.6 和 PHP 7.4,Laravel 8.49

我想念什么?

【问题讨论】:

  • 嘿,看来您使用的是 fakerphp 库,它没有国家格式化程序。相反,您可以使用国家代码(2 个字母或 3 个字母)。在此处查看更多详细信息fakerphp.github.io/formatters/miscellaneous/#countrycode
  • @koussay 把它写下来作为答案,因为它似乎是解决方案:)

标签: laravel unit-testing phpunit laravel-8 laravel-testing


【解决方案1】:

这样试试

<?php
namespace Database\Factories;
use App\Models\Settings\Lang;
use Illuminate\Database\Eloquent\Factories\Factory;
class LangFactory extends Factory
{
    protected $model = Lang::class;
    public function definition()
    {
        return [
            'name' => $this->faker->country(),
            'code' => $this->faker->languageCode(),
        ];
    }
}

它应该可以工作

【讨论】:

    【解决方案2】:

    看来您正在使用 fakerphp 库,它没有国家格式化程序。相反,您可以使用国家代码(2 个字母或 3 个字母)。在这里查看更多详情。 https://fakerphp.github.io/formatters/miscellaneous/#countrycode

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2017-11-16
      • 2020-03-05
      • 2023-03-16
      • 1970-01-01
      • 2017-05-24
      • 2020-03-24
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多