【发布时间】:2019-07-03 05:21:49
【问题描述】:
我可以将我的应用程序中的faker 区域设置在config/app.php 更改为pt_BR 更改'faker_locale' => 'pt_BR',,它在我的工厂中运行良好,但在我的测试用例中却不行。这就是我在我的测试中导入 faker 的方式:
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);
}
测试将在$this->faker->cellPhoneNumber 中失败,因为它在默认语言环境中不可用。我正在使用 Laravel 5.8 和 PHP 7.2
【问题讨论】:
-
您应该检查一下
WithFakerTrait 为您提供的开箱即用的功能。您可能需要在自己的setUp()方法中修改 faker 实例。