需要登录的测试方法

时间:2020-03-23 09:23:17

标签: php laravel unit-testing testing

我想测试诸如user之类的方法(方法需要登录user)。

$user = User::find(1);
$response = $this->actingAs($user)->json('POST', '/store/post', [
            'title' => 'Hello World',
            'description' => 'Hello World. We are Anonymous.',
            'user_id' => 1
]);

$response
    ->assertStatus(201)
    ->assertJson([
        'created' => true,
    ]);

我有此错误(我不知道我可以添加所有错误,所以我将部分内容显示出来):

1) Tests\Feature\ExampleTest::testBasicExample

Illuminate\Database\QueryException: could not find driver (SQL: PRAGMA foreign_keys = ON;)
//...
Caused by
Doctrine\DBAL\Driver\PDOException: could not find driver
//...
Caused by
PDOException: could not find driver

如果我尝试在文档上使用Factory,则会遇到同样的问题。我该如何解决我的问题?

2 个答案:

答案 0 :(得分:0)

我认为您必须在应用程序中安装以下软件包。 尝试运行

  composer update

  composer require doctrine/dbal

您可能还需要安装php-mysql扩展名。

尝试运行

sudo apt-get install php-mysql.

答案 1 :(得分:0)

我相信您正在使用sqlite模拟PHPUnit中的数据库 如果是php5

sudo apt-get install php5-sqlite

of php7

sudo apt-get install php7-sqlite

比重新启动apache服务器

sudo service apache2 restart