PHPUnit中的错误:Laravel 5.4版本

3

我需要在 Laravel 中使用单元测试,但需要下载库 laravel/browser-kit-testing。当我下载它时,它告诉我需要 PHP 5.6,而我正在使用 PHP 5.4。

 public function testBasicTest()
    {
        $this->visit('/home')
            ->seePageIs('/login');
        $response = $this->call('GET', '/dradmin');
        $this->assertEquals(200, $response->status());
        $this->visit('/login')
            ->type('mahmoud@mahmoud.com', 'email')
            ->type('123456', 'password')
            ->press('Login')
            ->seePageIs('/home');

    }
}

当使用 phpunit

Error: Call to undefined method Tests\Feature\UserTest::visit()

/Users/mahmoud310/ecare/tests/Feature/UserTest.php:21

我在其他问题中看到了这个内容:Laravel 5.4 HTTP测试 - 方法seeInElement

使用命令 laravel/browser-kit-testing 可以解决问题。

但在我的情况下没有起作用。

Using version ^1.0 for laravel/browser-kit-testing
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - The requested package phpunit/phpunit (locked at 5.7.19, required as 4.8.*) is satisfiable by phpunit/phpunit[5.7.19] but these conflict with your requirements or minimum-stability.
  Problem 2
    - Conclusion: don't install phpunit/phpunit 4.8.35
    ...
    - Conclusion: don't install phpunit/phpunit 4.8.1
    - phpunit/phpunit 4.8.0 conflicts with phpunit/phpunit-mock-objects[3.4.3].
    ...
    - phpunit/phpunit 4.8.0 conflicts with phpunit/phpunit-mock-objects[3.4.3].
    - Installation request for phpunit/phpunit 4.8.* -> satisfiable by phpunit/phpunit[4.8.0, 4.8.1, ..., 4.8.9].
    - Installation request for phpunit/phpunit-mock-objects (locked at 3.4.3) -> satisfiable by phpunit/phpunit-mock-objects[3.4.3].


Installation failed, reverting ./composer.json to its original content.

从您的项目文件夹中运行 ./vendor/phpunit/phpunit/phpunit - Little Phild
不工作!显示相同的错误。 - mahmoud310
你执行了 composer install 命令来安装 Laravel 依赖吗? - Little Phild
是的,composer update 最后一次更改。 - mahmoud310
我可以在 Laravel 5.4 中使用旧的方法。 - mahmoud310
显示剩余4条评论
3个回答

3
在终端中输入。
composer require laravel/browser-kit-testing --dev

然后在您的项目中使用下载的文件。
use Tests\CreatesApplication;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

别忘了从BaseTestCase继承。

示例代码

<?php

namespace Tests\Feature;

use Tests\CreatesApplication;
use Tests\TestCase;
use App\User;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

class UserTest extends BaseTestCase
{
    use CreatesApplication;
    public $baseUrl = 'http://localhost';
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {


        $this->visit('/home')
            ->seePageIs('/login');
        

    }
}

2

BrowserKit为Laravel 5.4提供了向后兼容的层,用于对Laravel 5.3风格的“BrowserKit”测试进行测试。

首先,安装此软件包:

composer require laravel/browser-kit-testing --dev

接下来,修改您的应用程序的基本TestCase类,将其扩展为Laravel\BrowserKitTesting\TestCase而不是Illuminate\Foundation\Testing\TestCase:

然后,您的userTest.php应该如下所示:

<?php

  namespace Tests;

  use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

  abstract class TestCase extends BaseTestCase
  {
      use CreatesApplication;

      public $baseUrl = 'http://localhost';

      //your codes here ...
 }

但是当输入phpunit时,在终端中显示:未执行任何测试! - mahmoud310

0

你无法使用这个。Laravel 5.4需要PHP 5.6或更高版本 (证明在此),因此它与浏览器套件没有任何共同之处。最后一个支持PHP 5.4的Laravel版本是5.0 (证明在此)。

所以,要么你不知道自己正在使用哪个版本的PHP,要么问题出在其他地方。正如你所建议的那样,在PHP 5.4上安装Laravel 5.1+是不可能的。


我认为Laravel 5.4与所有的PHP 5.6+兼容,因为我正在使用PHP 7.1.3Laravel 5.4,并且一切工作正常。 - Little Phild
@LittlePhild 是的,但是OP写道他正在使用PHP 5.4,而这根本不受支持。 - Marcin Nabiałek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接