Laravel phpunit - get()方法未定义

mmvthczy  于 2023-03-28  发布在  PHP
关注(0)|答案(1)|浏览(109)

我尝试在Laravel 5.6中使用phpunit,得到以下错误:

$ phpunit

1) Tests\Feature\ExampleTest::testBasicTest
Error: Call to undefined method Tests\Feature\ExampleTest::get()

/Users/xxxx/dev/xxxx/tests/Feature/ExampleTest.php:12

我在tests/Feature/ExampleTest.php的代码

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
  public function testBasicTest()
  {
    $response = $this->get('/');
    $response->assertStatus(200);
  }
}

我的代码在tests/TestCase.php

<?php

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
// use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
  public function setUp():void { parent::setUp(); }
  public function tearDown():void { parent::tearDown(); }
  use CreatesApplication;
}

我做错什么了吗?

tjjdgumg

tjjdgumg1#

正如一位用户所说,您正在使用基础PHPUnit类,您必须使用Laravel类,因为它添加了用于测试的内容...因此您的代码应该如下所示:

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
  public function setUp():void { parent::setUp(); }
  public function tearDown():void { parent::tearDown(); }
  use CreatesApplication;
}

请,做lint/格式化你的代码,遵循标准...

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

在这里,没有必要覆盖setUptearDown,因为您正在调用父级,这与没有在此文件中定义它们完全相同...

相关问题