【发布时间】:2020-08-08 18:56:37
【问题描述】:
所以我正在学习为我的应用程序进行测试以及它不想通过的测试之一,这是逻辑:基本上,当用户请求主页时,我希望数据库列表计数将是 0,并且通过了,那么我还希望会话有一个错误键 NoBook 并且在这里它失败了。这是我尝试过的代码:
class BookDisplayManagmentTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function Show_error_message_when_there_is_no_book_to_display_in_index_page()
{
//Request the home page
$response = $this->get(route('home'));
// I expect the count on the database book equal 0
$this->assertCount(0, book::all());
//Then I also expect that the session will flash an error with key NoBook
$response->assertSessionHasErrors('NoBook');
}
}
但我收到此错误的问题是:
Session is missing expected key [errors]. Failed asserting that false is true.
以及添加会话错误的代码:
<?php
namespace App\Http\Controllers;
use App\Books;
use Illuminate\Http\Request;
class IndexController extends Controller
{
/** @show index function */
public function index()
{
$book = Books::paginate(7);
if(!$book->count())
{
session()->now('NoBook','There is no books at the moment');
}
return view('index', compact('book'));
}
}
【问题讨论】:
-
您能分享将错误添加到会话中的代码吗?
-
if(!$Book->count()) { session()->now('NoBook','暂时没有书'); }
-
请将代码添加到您最初的问题中????
-
尝试在
$response->assertSessionHasErrors('NoBook');之前添加$this->assertSessionHasErrors(); -
@ChristopheHubert 我确实添加了代码
标签: laravel phpunit laravel-7 laravel-testing