【问题标题】:Laravel phpUnit Test Error with more than one function int he Test classLaravel phpUnit 测试错误,测试类中有多个函数
【发布时间】:2016-05-21 11:52:06
【问题描述】:

我的测试类很简单

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->visit('/');

    }

    // when I add this, I get an error
    public function testAnotherExample()
    {
        $this->visit('profile');

    }
}

当我只有“testBasicExample”方法时,测试运行良好。但是,只要我添加“testAnotherExample”,测试就会失败并显示以下错误消息。

Fatal error: Cannot redeclare formatBytes() (previously declared in C:\xampp\htdocs\laravellab\helpers\functions.php:3) in C:\xampp\htdocs\laravellab\helpers\functions.php on line 7

Fatal error: Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.' in C:\xampp\htdocs\laravellab\vendor\laravel\framework\src\Illuminate\Container\Container.php:749
Stack trace:
#0 C:\xampp\htdocs\laravellab\vendor\laravel\framework\src\Illuminate\Container\Container.php(631): Illuminate\Container\Container->build('Illuminate\\Cont...', Array)
#1 C:\xampp\htdocs\laravellab\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(674): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#2 C:\xampp\htdocs\laravellab\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(154): Illuminate\Foundation\Application->make('Illuminate\\Cont...')
#3 C:\xampp\htdocs\laravellab\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(79): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#4 C:\xampp\htdocs\laravellab\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 749

如果我注释掉“testBasicExample”,相同的测试,那么另一个测试就可以了。

感谢您的帮助。

【问题讨论】:

  • helpers\functions.php 看起来像您自己的文件。你是如何加载它的,它包含什么?
  • 宾果游戏!感谢您的审查。它有一些共同的功能。将其包含功能从 include 更改为 include_once 解决了问题。

标签: unit-testing laravel


【解决方案1】:

上面的 PHPUnit 5.3.2 与 Laravel 5 配合得很好。

您需要先卸载旧的phpunit

sudo apt-get purge phpunit
sudo apt-get purge --auto-remove phpunit

然后安装 PHPUnit 5.6.1 如下

wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
sudo mv phpunit.phar /usr/local/bin/phpunit
phpunit --version

现在你可以运行 phpunit !!!

【讨论】:

    【解决方案2】:

    @ceejayoz 的回答是:

    Fatal error: Cannot redeclare formatBytes() (previously declared in C:\xampp\htdocs\laravellab\helpers\functions.php:3) in C:\xampp\htdocs\laravellab\helpers\functions.php on line 7
    

    是罪魁祸首。

    但是,从 include 到 include_once 的解决方案只是检查 include 只声明一次,这是在程序 PHP 中包含文件的经典方法。

    我建议自动加载文件,而不是更改您的 composer.json 文件。只需在自动加载部分中包含逻辑文件路径(composer.json 文件所在的位置)

    "autoload": {
        "files": ["helpers\functions.php"]
    
    },
    

    https://getcomposer.org/doc/04-schema.md#files

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 2016-06-12
      • 2017-10-19
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2012-07-01
      • 2015-06-01
      • 1970-01-01
      相关资源
      最近更新 更多