【问题标题】:Error in phpunit laravel 5.4phpunit laravel 5.4 中的错误
【发布时间】:2017-10-02 09:18:41
【问题描述】:

我需要在 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 testing - method 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
  • 不行!显示相同的错误
  • 你有没有 composer install 安装 laravel 依赖项
  • 是的,作曲家更新最后一次更改
  • 我建议如果您不使用宅基地,您可以使用本指南进行设置 Installing HomeStead

标签: unit-testing laravel-5 phpunit composer-php laravel-5.4


【解决方案1】:

在终端输入

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 时显示:没有执行测试!
    【解决方案3】:

    你对此无能为力。 Laravel 5.4 需要 PHP 5.6 或更高版本 (proof here),因此它与浏览器套件没有任何共同之处。最后一个支持 PHP 5.4 的 Laravel 是 5.0 版(proof here).

    所以要么你不知道你正在使用什么版本的 PHP,要么问题出在其他地方。按照您的建议,应该不可能在 PHP 5.4 上安装 Laravel 5.1+。

    【讨论】:

    • 我认为laravel 5.4 与所有php 5.6+ 兼容,因为我将PHP 7.1.3Laravel 5.4 一起使用,并且效果很好
    • @LittlePhild 是的,但 OP 写道他使用的是 PHP 5.4,这根本不受支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2017-12-26
    • 2017-12-26
    • 2017-12-11
    • 2017-11-14
    • 2017-09-04
    相关资源
    最近更新 更多