Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 放射性气体测试方法存在的问题_Laravel_Testing_Phpunit - Fatal编程技术网

Laravel 放射性气体测试方法存在的问题

Laravel 放射性气体测试方法存在的问题,laravel,testing,phpunit,Laravel,Testing,Phpunit,我有以下代码: /** @test */ public function testBasicExample() { $user = User::find(1); // $user = factory(User::class)->create(); $response = $this->actingAs(User::find(1))->json('POST', '/store/ad', [ 'title' => 'H

我有以下代码:

/**  @test */
public function testBasicExample()
{
    $user = User::find(1);
    // $user = factory(User::class)->create();
    $response = $this->actingAs(User::find(1))->json('POST', '/store/ad', [
                'title' => 'Hello World',
                'city' => 1,
                'phone' => '666555555',
                'description' => 'fasd asd as d asd as d asd as d asd as d asd as d asd as da sd asd',
                'user_id' => 1
    ]);

    $response
        ->assertStatus(201)
        ->assertJson([
            'created' => true,
        ]);
}
不幸的是,此时此刻我遇到了第一个问题。它无法查看用户表

Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1 没有这样的表:users(SQL:select*from“users”,其中“users”。“id”= 1(限制1)

我正在寻找如何解决我的问题,我发现我必须使用数据库迁移。所以我补充说

use Illuminate\Foundation\Testing\DatabaseMigrations;
class ExampleTest extends TestCase
{
    use DatabaseMigrations;
//...
}
但现在我有了新的问题

TypeError:参数1传递给 Illumb\Foundation\Testing\TestCase::actingAs()必须实现 接口Illumb\Contracts\Auth\Authenticatable,给定空值

所以我实现了这一点

use Illuminate\Contracts\Auth\Authenticatable;
class ExampleTest extends TestCase
{
    use DatabaseMigrations;
    use Authenticatable;
//...
}
它生成了新的错误:

Tests\Feature\ExampleTest无法使用 Illumb\Contracts\Auth\Authenticatable-这不是特征

我怎样才能解决我的问题?我如何测试它

@编辑 我发现了问题,但我不知道为什么它不起作用。我有这条规矩来对付这个城市

'city' => 'required|integer|exists:cities,id'

问题是最后一条规则:exists:cities,id。我尝试了不同的id exists cities and nothing work。

问题是
DatabaseMigrations
特性将在每次测试后重置数据库,因此在测试运行时数据库中没有用户

这意味着您当前正在下一行传递
null

$this->actingAs(User::find(1))
您必须首先使用
factory
helper创建用户:

$user = factory(User::class)->create();
以下应该可以解决您的问题:

1-拆下以下部件:

use Authenticatable;
不知道您为什么添加了这个,异常清楚地表明传递给
$this->actingAs()
的参数必须实现
Authenticatable
接口,而不是当前类

2-将测试更改为以下内容:

/**  @test */
public function testBasicExample()
{
    $this->actingAs(factory(User::class)->create())
        ->json('POST', '/store/ad', [
            'title' => 'Hello World',
            'city' => 1,
            'phone' => '666555555',
            'description' => 'fasd asd as d asd as d asd as d asd as d asd as d asd as da sd asd',
            'user_id' => 1
        ])
        ->assertStatus(201)
        ->assertJson(['created' => true]);
}

我不知道您在这条路线上做什么,所以我无法告诉您为什么会收到此状态代码,但它可能与验证有关,请尝试添加
$this->withoutExceptionHandling()在测试开始时,检查错误。我发现了问题,但不知道如何解决。我把它展示在我的电脑上post@Kowal8856这与用户的错误相同,数据库是空的,没有城市,您必须首先为您的测试创建一个城市。