【问题标题】:Getting InvalidArgumentException - Unknown format from fakerphp/faker library in laravel while testing in custom testsuite using pest获取 InvalidArgumentException - laravel 中的 fakerphp/faker 库中的未知格式,同时使用害虫在自定义测试套件中进行测试
【发布时间】:2022-01-03 21:25:11
【问题描述】:

我正在使用 Pest 库在 laravel 中编写测试。我在 laravel 中创建了 my-laravel-application/tests/Integration 目录 并在phpunit.xml中定义了一个新的测试套件

<testsuite name="Integration">
   <directory suffix=".test.php">./tests/Integration</directory>
</testsuite>

这样 laravel 会确认集成目录中的测试文件,我可以将集成测试写在一个具有正确名称的单独目录中(集成目录),然后我将测试文件放在 my-laravel-application/tests/Integration 目录中,我得到以下运行时出错php artisan test:

InvalidArgumentException - Unknown format "name"
vendor/fakerphp/faker/src/Faker/Generator.php:657

这表明我的UserFactory 中的$this-&gt;faker-&gt;name() 代码行(我在测试中使用UserFactory 类)有问题,它表示$this->faker 上不存在name() 方法。但是我的测试在将它们移动到my-laravel-application/tests/Integration 目录之前运行良好。真正的问题是什么?我该如何解决?

【问题讨论】:

    标签: php laravel phpunit pestphp fakerphp


    【解决方案1】:

    我找到了基于this answer的解决方案。你应该强制使用Tests\TestCase(而不是PHPUnit\Framework\TestCase)来编写在你新创建的目录(集成目录)中的所有测试,方法是将以下代码添加到/tests/Pest.test:

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

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 2021-12-11
      • 2021-06-28
      • 2023-04-04
      • 2018-10-23
      • 2021-11-29
      • 2019-02-16
      • 1970-01-01
      • 2017-11-16
      相关资源
      最近更新 更多