Error in phpunit laravel 5.4



我需要在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 TestsFeatureUserTest::visit()
/Users/mahmoud310/ecare/tests/Feature/UserTest.php:21

我读了其他问题Laravel 5.4 HTTP测试 - 方法参见

使用命令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.

在终端中输入

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

然后使用项目中的下载文件

use TestsCreatesApplication;
use LaravelBrowserKitTestingTestCase as BaseTestCase;

,不要忘记从BaseTestCase延伸。

示例代码

<?php
namespace TestsFeature;
use TestsCreatesApplication;
use TestsTestCase;
use AppUser;
use IlluminateFoundationTestingWithoutMiddleware;
use IlluminateFoundationTestingDatabaseMigrations;
use IlluminateFoundationTestingDatabaseTransactions;
use LaravelBrowserKitTestingTestCase 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');
        
    }
}

BrowserKit提供了laravel 5.3样式" browserkit"测试的向后兼容层。

首先,安装此软件包:

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

接下来,修改您的应用程序的基本测试柜类以扩展LaravelBrowserKitTestingTestCase而不是IlluminateFoundationTestingTestCase:

,您的userTest.php应该是

<?php
  namespace Tests;
  use LaravelBrowserKitTestingTestCase as BaseTestCase;
  abstract class TestCase extends BaseTestCase
  {
      use CreatesApplication;
      public $baseUrl = 'http://localhost';
      //your codes here ...
 }

您不能对此做任何事情。Laravel 5.4需要PHP 5.6或更高版本(在此处证明(,因此与浏览器套件没有任何共同点。支持PHP 5.4的最后一个Laravel是5.0版(在此处证明(。

因此,您要么不知道您正在使用哪种版本的PHP,要么问题在其他地方。如您所建议

最新更新