Laravel – 如何在PHPUnit测试中使用faker?

前端之家收集整理的这篇文章主要介绍了Laravel – 如何在PHPUnit测试中使用faker?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在运行测试时给出了这个错误

undefined variable $faker.

这是WithFaker文件.

https://github.com/laravel/framework/blob/5.5/src/Illuminate/Foundation/Testing/WithFaker.php

  1. <?PHP
  2.  
  3. namespace Tests\Unit;
  4.  
  5. use App\User;
  6. use Tests\TestCase;
  7. use Illuminate\Foundation\Testing\WithFaker;
  8. use Illuminate\Foundation\Testing\RefreshDatabase;
  9.  
  10. class LoginTest extends TestCase
  11. {
  12.  
  13. use WithFaker;
  14.  
  15. /**
  16. * A basic test example.
  17. *
  18. * @return void
  19. */
  20.  
  21. /** @test */
  22. public function test_example()
  23. {
  24.  
  25. $user = User::create([
  26. 'username' => $faker->firstName(),]);
  27.  
  28. }
  29.  
  30. }
你必须使用$this-> faker-> firstName()而不仅仅是$faker-> firstName()

猜你在找的Laravel相关文章