Uczę się pisania testów w Laravelu. Jako że nie wiem jak powinny wyglądać "profesjonalnie" - to chciałem dopytać czy to, co robię ma sens

1. Test 1 - test sprawdzający czy strona działa poprawnie
class HomeTest extends TestCase { public function testHomePageWorkCorrectly() { //$this->withoutExceptionHandling(); $response = $this->get('/'); $response->assertStatus(200); } }
2. Test 2 - test logowania z dobrym i błędnym hasłem
class LoginTest extends TestCase { public function testUserCanLoginWithCorrectPassword() { //$this->withoutExceptionHandling(); $response = $this->post(route('cms.check_admin_login'), [ 'email' => 'lukpeta@icloud.com', 'password' => 'passw' ]); $response->assertRedirect('/psCMS/'); $this->assertTrue(Auth::check()); } public function testUserCannotLoginWithInCorrectPassword() { //$this->withoutExceptionHandling(); $response = $this->post(route('cms.check_admin_login'), [ 'email' => 'lukpeta@icloud.com', 'password' => 'xxxxx' ]); $response->assertRedirect('psCMS/login?error=1'); $this->assertFalse(Auth::check()); } }
3. Test 3 - test modułu reklam. Dodawanie nowej reklamy, sprawdzanie czy istnieje, pobieranie listy reklam, sprawdzanie czy walidacja pól działa, kasowanie
class AdTest extends TestCase { use \Illuminate\Foundation\Testing\WithFaker; public function testCreateAd() { $user = User::find(1); $this->be($user); $ad = factory(Ad::class)->create(); $this->post(route('ads.edit') . '/' . $ad->id, $ad->toArray()); $this->assertDatabaseHas('ads', ['id' => $ad->id]); } public function testAdExist() { $user = User::find(1); $this->be($user); $ad = factory(Ad::class)->create(); $this->post(route('ads.edit') . '/' . $ad->id, $ad->toArray()); $this->assertDatabaseHas('ads', ['id' => $ad->id]); } public function testGetAdsList() { $user = User::find(1); $this->be($user); $ads = factory(Ad::class)->create(); $response = $this->get(route('ads.index')); $response->assertSee($ads->title); } public function testCreateAdMustBeCompleted() { $user = User::find(1); $this->be($user); $response = $this->actingAs($user)->post(route('ads.store'), [ 'title' => null, 'provincial_id' => null, 'content' => null, ]); $response->assertSessionHasErrors(['title', 'provincial_id', 'content']); } public function testCreateAdFromForm() { $user = User::find(1); $this->be($user); $faker = Faker\Factory::create('pl_PL'); $title = $faker->sentence($nbWords = 6, $variableNbWords = true); $response = $this->actingAs($user)->post(route('ads.store'), [ 'title' => $title, 'content' => $faker->text($maxNbChars = 200), 'provincial_id' => $faker->numberBetween(1, 8), ]); $this->assertDatabaseHas('ads', ['title' => $title]); } public function testEditAdFromForm() { $this->withExceptionHandling(); $user = User::find(1); $this->be($user); $faker = Faker\Factory::create('pl_PL'); $ad = factory(Ad::class)->create(); $id = $ad->id; $title = $faker->sentence($nbWords = 6, $variableNbWords = true); $response = $this->actingAs($user)->post(route('ads.update') . '/' . $id, [ 'title' => $title, 'content' => $faker->text($maxNbChars = 200), 'provincial_id' => $faker->numberBetween(1, 8), 'id' => $id ]); //dd($response->getContent()); $this->assertDatabaseHas('ads', ['title' => $title, 'id' => $id]); } public function testDeleteAd() { //$this->withExceptionHandling(); //$this->withoutExceptionHandling(); $user = User::find(1); $this->be($user); $ad = factory(Ad::class)->create(); $id = $ad->id; $faker = Faker\Factory::create('pl_PL'); $title = $faker->sentence($nbWords = 6, $variableNbWords = true); $response = $this->actingAs($user)->post(route('ads.destroy'), [ 'title' => $title, 'content' => $faker->text($maxNbChars = 200), 'provincial_id' => $faker->numberBetween(1, 8), //'id'=> $id, 'id' => [$id] ]); $this->assertDatabaseMissing('ads', ['id' => $ad->id]); } }
Czy takie testy są poprawne? Czy raczej powinny wyglądać inaczej?
