【问题标题】:Laravel 8 not running testsLaravel 8 没有运行测试
【发布时间】:2021-04-21 13:24:36
【问题描述】:

首先,我现在的环境:

  • Docker 版本 20.10.1
  • docker-compose 版本 1.27.4
  • MySql 8.0.21(Docker 镜像)
  • PHP 7.4.13 - Docker 映像
  • PHP 7.4.13 本地

作曲家.json:

"require": {
    "php": "^7.4|^8.0",
    "doctrine/dbal": "^3.0",
    "fideloper/proxy": "^4.4",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "7.0",
    "laravel-lang/lang": "~8.0",
    "laravel/framework": "^8.12",
    "laravel/tinker": "^2.5",
    "ramsey/uuid": "^4.1"
},
"require-dev": {
    "barryvdh/laravel-ide-helper": "^2.9",
    "facade/ignition": "^2.5",
    "fakerphp/faker": "^1.9.1",
    "laravel/breeze": "^1.0",
    "mockery/mockery": "^1.4.2",
    "nunomaduro/collision": "^5.0",
    "phpunit/phpunit": "^9.3.3"
}

我在运行时收到以下错误:

  • $ php 工匠测试

PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: 
Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. 
in /home/______/proj/php//______/vendor/laravel/framework/src/Illuminate/Container/Container.php:1038

我的测试文件(我的测试还没写完):

<?php

namespace Tests\Unit;

use App\Models\Pessoa;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class __________ extends TestCase
{
    use RefreshDatabase;
    protected Pessoa $pessoa;
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->pessoa = factory(Pessoa::class);

        $this->assertTrue(true);
    }
}

【问题讨论】:

    标签: php laravel testing phpunit


    【解决方案1】:

    嗯,“解决方案”有点简单,在搜索了很多之前,我在GitHub issues上找到了以下语句:

    • “对我来说,它原来是访问权限。这可能是 vm/container 环境的问题。”

    我尝试使用 SU 运行它,但又遇到了另一个错误:

    • $ sudo php 工匠测试

    
       FAIL  Tests\Unit\____________
      ⨯ example
    
      ---
    
      • Tests\Unit\_____________ > example
       Illuminate\Database\QueryException 
    
      SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
    
      at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
        674▕         // If an exception occurs when attempting to run a query, we'll format the error
        675▕         // message to include the bindings with SQL, which will make this exception a
        676▕         // lot more helpful to the developer instead of just the database's errors.
        677▕         catch (Exception $e) {
      ➜ 678▕             throw new QueryException(
        679▕                 $query, $this->prepareBindings($bindings), $e
        680▕             );
        681▕         }
        682▕ 
    
    
      Tests:  1 failed
      Time:   0.12s
    

    嗯,它也不起作用,但是因为我是使用 docker 在本地运行这个项目,所以我尝试在 Container 中运行它,它起作用了!

    • $ docker exec f546517976f4 php artisan test

    TL;DR : 在 Docker 容器中运行测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-14
      • 2019-02-13
      • 2013-07-29
      • 2018-07-08
      • 2018-06-01
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多