我在运行测试时给出了这个错误:
undefined variable $faker.
这是WithFaker文件.
https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Testing/WithFaker.php
- <?PHP
- namespace Tests\Unit;
- use App\User;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\WithFaker;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- class LoginTest extends TestCase
- {
- use WithFaker;
- /**
- * A basic test example.
- *
- * @return void
- */
- /** @test */
- public function test_example()
- {
- $user = User::create([
- 'username' => $faker->firstName(),]);
- }
- }
你必须使用$this-> faker-> firstName()而不仅仅是$faker-> firstName()