【发布时间】:2018-10-29 17:31:40
【问题描述】:
我正在使用 Laravel 5.4 和 PHPUnit 6.5.5。在我的测试中使用 Laravel 的 assertDatabaseHas() 方法时,我不断收到以下错误。
Error: Call to undefined method Illuminate\Http\Response::assertDatabaseHas()
这是我使用assertDatabaseHas()的代码片段:
$response = $this->withSession(['user_id' => $this->user_id])
->json('post',
route('some_route'),
$request //an array
);
$request['myuser_id'] = $this->user_id;
$response->assertStatus($expected['code'])
->assertDatabaseHas('profiles',$request);
我也尝试使用$this->assertDatabaseHas(),但是出现了新的错误:
TypeError: Argument 2 passed to PHPUnit\Framework\Assert::assertThat() must be an instance of PHPUnit\Framework\Constraint\Constraint, instance of Illuminate\Foundation\Testing\Constraints\HasInDatabase given, called in /project_path/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php on line 22
我搜索了一会儿问题的解决方案,找到了 this question ,但是将 PHPUnit 版本降级到 5.* 对我不起作用。
【问题讨论】:
-
dd($request);的结果是什么? -
$request 只是一个关联数组 ['id' => 2 , 'name' => 'test' , ....] @JonasStaudenmeir
-
你的 Laravel 版本是什么?
-
5.4.36 @JonasStaudenmeir
-
PHPUnit 5.* 是否出现同样的错误?
标签: php laravel unit-testing phpunit