Answer the question
In order to leave comments, you need to log in
How to do functional testing in Laravel?
Hello. There are many examples on the Internet of how to conduct functional testing. I learned the basic methods and learned how to apply, write tests. Everything goes as I expected and all is well. But now the question arises: what area of routes should tests cover?
Let's start with POST requests. My project (browser online text game) cannot exist without the initial data in the database, so one of the main and first tests is to check that all the initial (base) tables are filled in as they should, for this I use SeederTest:
public function completeness()
{
$this->assertDatabaseCount('base_items', base_items::getCount());
}
public function SuccessReg()
{
$this->get('/');
$response = $this->post(route('register'), ['login' => 'user', 'password' => '123', 'password_confirmation' => '123']);
$this->assertAuthenticated();
$this->assertDatabaseHas('users', ['login' => 'user']);
$response->assertLocation('/home');
}
/** @test */
public function InvalidLogin()
{
$this->get('/');
$response = $this->post(route('register'), ['login' => '', 'password' => '123', 'password_confirmation' => '123']);
$this->assertGuest();
$this->assertDatabaseMissing('users', ['login' => '']);
$response->assertLocation('/');
}
public function test_home_page()
{
\Auth::login(User::factory()->create());
$response = $this->get('/home');
$response->assertViewIs('auth.home');
$response->assertViewHasAll(['me', 'buildings']);
$response->assertStatus(200);
}
Answer the question
In order to leave comments, you need to log in
Am I checking everything correctly?
If somewhere when passing data to the view an error occurs (something from the database is not found), will there be an error here as well?
Another question, is there a way to log in the user to the entire test class at once so as not to register login every time?
All that I have demonstrated is the whole point of functional tests, isn't it?
Why is there no letter Y on the green button on top?) Because the Toaster asks ONE specific question. And not 15. I will write in general. I write tests before development, so the question of choosing a "coverage" is not worth it. I recommend this option. Yes, you write more code, but the development time is shorter as a result. development is a constant debugging of code and logic, which, without tests, needs to be done by hand. Plus, the quality / safety of the code and tests is much higher, there is a detailed well-thought-out technical specification (in tests), documentation and a free head. According to the structure, I first write setUp where I set the necessary data, create objects, etc., and then tests. Duplicate code within the class I take out in methods. Within all tests - in TestCase or traits. Each test (script) starts the logic from scratch, but sometimes I link it via @depends.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question