我是 Laravel 的新手,但我不明白如何在 TestCase 中查看視圖。
目前我在我的 TestCase 類中有這段代碼:
<?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', [但我遇到了同樣的錯誤。
我想測驗視圖,因為我認為這是檢測我的提交是否有錯誤的最準確方法,因為無論我是否有錯誤,渲染的視圖都不相同。
這是我的控制器類:
<?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);
我錯過了什么?您需要更多代碼來回答嗎?
uj5u.com熱心網友回復:
您可以禁用例外處理以查看結果。
/**
* Test page HTTP status
*
* @return void
*/
public function test_status()
{
$this->withoutExceptionHandling();
$response = $this->get('/contact');
$response->assertStatus(200);
}
也許會有用Laravel 單元測驗(回應不是視圖)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/447575.html
