【问题标题】:laravel unit testing not giving right response with "phpunit"laravel 单元测试没有用“phpunit”给出正确的响应
【发布时间】:2016-07-13 08:53:32
【问题描述】:

我需要为我的 laravel 应用程序进行单元测试。到目前为止,我已经开始编写测试代码,但是当我运行它时,它返回的结果不是我所期望的,到目前为止我还没有在互联网上的任何地方看到这个。所以我一定是在做一些愚蠢的事情或错过了什么,对吧?

我在 tests/tests.php 中的测试类

<?php

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

class Tests extends TestCase
{
public function testLoad(){
    $this->call('GET', '/');

    $this->assertResponseOk();

    $response = $this->action('GET', 'mainController@load');

    $this->visit('/')
         ->see('Todo seznam');

    $this->assertViewHas('tasks');
}

public function dbTest(){
    $this->call('GET', '/delete');

    $this->assertHasOldInput();
}
}

所以我假设我必须安装 phpunit(我在 ubuntu 16.04 上),然后我像其他成功但得到这个结果的人一样,在我的项目的根目录中使用“phpunit”运行它:

.................................................. ....................

Time: 70 ms, Memory: 4.00MB

PHP Fatal error:  Uncaught Error: Call to undefined method PHPUnit_Framework_TestResult::warningCount() in /usr/share/php/PHPUnit/TextUI/ResultPrinter.php:185
Stack trace:
#0 /home/matic/Documents/todo-app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(446): PHPUnit_TextUI_ResultPrinter->printResult(Object(PHPUnit_Framework_TestResult))
#1 /usr/share/php/PHPUnit/TextUI/Command.php(155): PHPUnit_TextUI_TestRunner->doRun(Array)
#2 /usr/share/php/PHPUnit/TextUI/Command.php(106): PHPUnit_TextUI_Command->run(Array, true)
#3 /usr/bin/phpunit(29): PHPUnit_TextUI_Command::main()
#4 {main}
  thrown in /usr/share/php/PHPUnit/TextUI/ResultPrinter.php on line 185

.................................................. ....................

一堆文件我什么都没做。

然后我想我会在那里创建一个新项目和 phpunit,但结果是一样的,但错误更多。

.................................................. ......................

Time: 107 ms, Memory: 10.00MB

PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Container/Container.php:748
Stack trace:
#0 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\Container\Container->build('Illuminate\\Cont...', Array)
#1 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(697): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#2 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(154): Illuminate\Foundation\Application->make('Illuminate\\Cont...')
#3 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(79): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#4 [internal function]: Illuminate\Foundation\Bootstrap\HandleE in /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 748
PHP Fatal error:  Uncaught Illuminate\Contracts\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Container/Container.php:748
Stack trace:
#0 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\Container\Container->build('Illuminate\\Cont...', Array)
#1 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(697): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
#2 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(154): Illuminate\Foundation\Application->make('Illuminate\\Cont...')
#3 /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(79): Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler()
#4 /home/matic/Documents/unit/vendor/laravel/framework/src/Illu in /home/matic/Documents/unit/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 748

.................................................. .....................

我认为这里有问题是不是错了?

我应该如何解决这个问题? 正常吗?

感谢您给我的任何帮助,我会很高兴的。

【问题讨论】:

  • Laravel 自带了自己的 PHPUnit 版本。只需尝试在您的项目目录中运行vendor/bin/phpunit
  • 看,我之前试过这个,但它对我不起作用,但现在我在全新安装中尝试了它并且它工作,但在我的项目中它说找不到命令,但文件在那里,一切都在
  • 我有点难过,没有人能帮我节省一些时间。但这里有。我知道问题出在哪里,但是我删除了供应商文件夹并运行了 composer install,它重新替换了它,现在它可以工作了。

标签: php unit-testing laravel


【解决方案1】:

我有点难过,没有人帮助可能会节省我一些时间。但这里有。我知道问题出在哪里,但是我删除了供应商文件夹并运行了 composer install,它替换了它,现在它可以工作了

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2021-08-22
    相关资源
    最近更新 更多