![](/img/trans.png)
[英]Unit testing with Laravel assertDatabaseHas and a float column
[英]laravel assertDatabaseHas() testing method doesn't work
我正在使用 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
我搜索了一段時間以尋找問題的解決方案並找到了這個問題,但是將 PHPUnit 版本降級到 5.* 對我不起作用。
assertDatabaseHas()
始終有效,您只是使用不正確,Laravel 版本的更改不會解決您的問題。
您在這里處理兩個獨立的實體——一個 HTTP 響應和一個數據庫條目——它們需要被這樣對待。 json()
或assertStatus()
等方法的返回是Illuminate\\Http\\Response
的實例,並且沒有數據庫方法。
將這些事情分開:發出 HTTP 請求並檢查響應,然后檢查數據庫。
$this
->withSession(['user_id' => $this->user_id])
->json('post', route('some_route'), $request)
->assertStatus($expected['code']);
$request['myuser_id'] = $this->user_id;
$this->assertDatabaseHas('profiles', $request);
我將 Laravel 版本從 5.4.37 升級到 5.5.40,問題解決了。 assertDatabaseHas() 現在工作正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.