所以我正在学习为我的应用程序进行测试以及它不想通过的测试之一,这是逻辑:基本上,当用户请求主页时,我希望数据库列表计数为 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'));
}
}