代码之家  ›  专栏  ›  技术社区  ›  sasori

如何修复laravel7.xphpunit中未定义的方法错误?

  •  0
  • sasori  · 技术社区  · 6 年前

    我遇到了奇怪的错误。就像我在网上看到的所有常见函数一样,我的phpunit中没有定义。。

    ./vendor/bin/phpunit
    

    我的代码是这样的

    namespace Tests\Unit;
    
        use PHPUnit\Framework\TestCase;
        use Illuminate\Foundation\Testing\DatabaseMigrations;
        use Illuminate\Foundation\Testing\WithoutMiddleware;
        
        
        class AppTest extends TestCase
        {
        
        
            use DatabaseMigrations;
            use WithoutMiddleware;
        
        
            /** @test */
            public function test_can_push_data() {
        
                $paramData = [
                    'param1' => 'value1',
                    'param2' => 'value2',
        
                ];
        
                $response = $this->json('POST', route('app.push'), $paramData);
        
                $response->assertStatus(200);
            }
        
        }
    

    我得到这个错误

    1) Tests\Unit\AppTest::test_can_push_data
    Error: Call to undefined method Tests\Unit\AppTest::json()
    

    我还注意到其他函数出现错误,我不知道为什么

    $this->post()
    $this->get()
    

    有什么想法吗?我在网上看到的大多数教程都在使用这些常用方法,这些方法会在我的代码中抛出错误。为什么?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Kurt Friars    6 年前

    你是在扩大错误的范围 TestCase Illuminate\Foundation\Testing\TestCase 班级。这将允许您的测试按照文档使用以下方法:

    namespace Tests\Unit;
    
        use Illuminate\Foundation\Testing\TestCase;
        use Illuminate\Foundation\Testing\DatabaseMigrations;
        use Illuminate\Foundation\Testing\WithoutMiddleware;
        
        
        class AppTest extends TestCase
        ...
    

    值得注意的是,创建自己的 测试用例 类,以提供公共自定义功能。它通常位于 Tests\TestCase