I'm newbie with Laravel but I don't understand how to get a view in TestCase.
Currently i've this code in my 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
}
}
As you can see I try with $response = $this->post('/contact', [
and $response = $this->call('POST', 'contact', [
but I've the same error.
I want to test view because I think this is the most accurate way to detect if my submission have error because the rendered view isn't same if I've an error or not.
This is my Controller 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'
]);
}
}
And this is my route file (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);
What I've missed ? Do you need more code to answer ?
CodePudding user response:
You can disable Exception Handling to see result.
/**
* Test page HTTP status
*
* @return void
*/
public function test_status()
{
$this->withoutExceptionHandling();
$response = $this->get('/contact');
$response->assertStatus(200);
}
Maybe will be useful Laravel Unit Test (The response is not a view)