Laravel 9 测试用例 "The response is not a view."
Laravel 9 TestCase "The response is not a view."
我是 Laravel 的新手,但我不明白如何在 TestCase 中获取视图。
目前我的测试用例中有这段代码 class :
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Mail\ContactMail;
use App\Models\Contact;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Http;
class ContactTest extends TestCase
{
use WithFaker;
private $appName;
protected function setUp(): void
{
parent::setUp();
$this->setUpFaker();
}
/**
* Test page HTTP status
*
* @return void
*/
public function test_status()
{
$response = $this->get('/contact');
$response->assertStatus(200);
}
/**
* Test form post
*
* @return void
*/
public function test_form_post()
{
$contactSubjects = Contact::all();
$messageContent = $this->faker->paragraphs(3, true);
// Invalid request
// $response = $this->post('/contact', [
$response = $this->call('POST', 'contact', [
'email' => $this->faker->text(), // invalid mail
'object' => $this->faker->numberBetween(count($contactSubjects)+10, count($contactSubjects)+100), // Be sure we have a number greater than last contact object id
'message_contact' => $messageContent
]);
$response->assertViewIs('contact'); // RESPONSE IS NOT A VIEW
// Valid request
// $response = $this->post('/contact', [
$response = $this->call('POST', 'contact', [
'email' => $this->faker->email(), // valid mail
'object' => $contactSubjects[rand(0, count($contactSubjects)-1)]->id, // Be sure we have a valid contact object ID
'message_contact' => $messageContent
]);
$response->assertViewIs('contact-validate'); // RESPONSE IS NOT A VIEW
}
}
如您所见,我尝试使用 $response = $this->post('/contact', [
和 $response = $this->call('POST', 'contact', [
,但我遇到了同样的错误。
我想测试视图,因为我认为这是检测我的提交是否有错误的最准确方法,因为无论我是否有错误,呈现的视图都不相同。
这是我的控制器class :
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreContactRequest;
use App\Models\Contact;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactMail;
class ContactController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$page = 'contact';
return view($page, [
'page' => $page,
'objects' => Contact::all(),
'error_sending' => false
]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreContactRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreContactRequest $request)
{
// Send mail
try {
Mail::to(config('mail.from.address'))->send(new ContactMail($request));
}
catch(\Throwable $e) {
return view('contact', [
'page' => 'contact',
'objects' => Contact::all(),
'error_sending' => 'Une erreur est survenue durant l\'envoie du mail. Merci de ré-essayer plus tard.<br><br>'.$e->getMessage()
]);
}
// Render view
return view('contact-validate', [
'page' => 'contact'
]);
}
}
这是我的路线文件 (web.php) :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
$page = 'home';
return view($page, ['page' => $page]);
});
Route::resource('/contact', ContactController::class);
我错过了什么?
您需要更多代码才能回答吗?
您可以禁用异常处理以查看结果。
/**
* Test page HTTP status
*
* @return void
*/
public function test_status()
{
$this->withoutExceptionHandling();
$response = $this->get('/contact');
$response->assertStatus(200);
}
也许会有用
我是 Laravel 的新手,但我不明白如何在 TestCase 中获取视图。
目前我的测试用例中有这段代码 class :
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Mail\ContactMail;
use App\Models\Contact;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Http;
class ContactTest extends TestCase
{
use WithFaker;
private $appName;
protected function setUp(): void
{
parent::setUp();
$this->setUpFaker();
}
/**
* Test page HTTP status
*
* @return void
*/
public function test_status()
{
$response = $this->get('/contact');
$response->assertStatus(200);
}
/**
* Test form post
*
* @return void
*/
public function test_form_post()
{
$contactSubjects = Contact::all();
$messageContent = $this->faker->paragraphs(3, true);
// Invalid request
// $response = $this->post('/contact', [
$response = $this->call('POST', 'contact', [
'email' => $this->faker->text(), // invalid mail
'object' => $this->faker->numberBetween(count($contactSubjects)+10, count($contactSubjects)+100), // Be sure we have a number greater than last contact object id
'message_contact' => $messageContent
]);
$response->assertViewIs('contact'); // RESPONSE IS NOT A VIEW
// Valid request
// $response = $this->post('/contact', [
$response = $this->call('POST', 'contact', [
'email' => $this->faker->email(), // valid mail
'object' => $contactSubjects[rand(0, count($contactSubjects)-1)]->id, // Be sure we have a valid contact object ID
'message_contact' => $messageContent
]);
$response->assertViewIs('contact-validate'); // RESPONSE IS NOT A VIEW
}
}
如您所见,我尝试使用 $response = $this->post('/contact', [
和 $response = $this->call('POST', 'contact', [
,但我遇到了同样的错误。
我想测试视图,因为我认为这是检测我的提交是否有错误的最准确方法,因为无论我是否有错误,呈现的视图都不相同。
这是我的控制器class :
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreContactRequest;
use App\Models\Contact;
use Illuminate\Support\Facades\Mail;
use App\Mail\ContactMail;
class ContactController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$page = 'contact';
return view($page, [
'page' => $page,
'objects' => Contact::all(),
'error_sending' => false
]);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\StoreContactRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreContactRequest $request)
{
// Send mail
try {
Mail::to(config('mail.from.address'))->send(new ContactMail($request));
}
catch(\Throwable $e) {
return view('contact', [
'page' => 'contact',
'objects' => Contact::all(),
'error_sending' => 'Une erreur est survenue durant l\'envoie du mail. Merci de ré-essayer plus tard.<br><br>'.$e->getMessage()
]);
}
// Render view
return view('contact-validate', [
'page' => 'contact'
]);
}
}
这是我的路线文件 (web.php) :
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
$page = 'home';
return view($page, ['page' => $page]);
});
Route::resource('/contact', ContactController::class);
我错过了什么? 您需要更多代码才能回答吗?
您可以禁用异常处理以查看结果。
/**
* Test page HTTP status
*
* @return void
*/
public function test_status()
{
$this->withoutExceptionHandling();
$response = $this->get('/contact');
$response->assertStatus(200);
}
也许会有用