Laravel-如何在phpunit测试中设置伪造的语言环境?

时间:2019-07-02 20:10:32

标签: laravel phpunit faker

我可以将faker中的应用程序中的config/app.php区域设置更改为pt_BR的{​​{1}}区域设置,它在我的工厂中可以正常工作,但在我的测试用例中却不能。这就是我在测试中导入仿造者的方式:

'faker_locale' => 'pt_BR',

测试将在namespace Tests\Unit; use Tests\TestCase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\RefreshDatabase; use App\Models\Proprietario; class ProprietarioTest extends TestCase { use WithFaker, RefreshDatabase; public function testStore(){ $attributes = [ 'name' => $this->faker->name, 'email' => $this->faker->email, 'address' => $this->faker->city, 'phone' => $this->faker->cellPhoneNumber, 'municipio' => $this->faker->randomDigit, ]; $response = $this->post('/api/v1/proprietario', $attributes); $response->assertStatus(201); $createdArea = Proprietario::latest(); $this->assertDatabaseHas('proprietarios', $attributes); } 中失败,因为它在默认语言环境中不可用。我正在使用Laravel 5.8和PHP 7.2

1 个答案:

答案 0 :(得分:1)

WithFaker特性为您提供了一种可以使用的方法

$this->faker('nl_NL')->postcode // dutch postcode

如果要在所有测试中使用它,请在测试中覆盖setupFaker

protected function setUpFaker()
{
    $this->faker = $this->makeFaker('nl_NL');
}
相关问题