newbie360's avatar

Write a new Test or modify old Test

For example:

A long long times ago, i have write a Test to ensure a contract page has Shop A details

OK, now a new commit added Shop B details on the page

A) modify the old Test to ensure Shop A and B details on the page

B) write a new Test in the current commit to ensure ONLY the Shop B details on the page

Which one is better, A or B ? Thanks for advice.

0 likes
3 replies
Tray2's avatar

That depends. If you have a test that ensure that Shop a exists in the view, and you then add Shop B in your database, you wouldn't really need a test for that, since it is the same show view being used, no matter how many shops you got. If you are talking about the index view, then you might want a test the ensures the order the shops are presented.

Here is an example with books:

Index :

it('lists books', function () {
    $genre = Genre::factory()->create([
        'media_type_id' => $this->mediaTypeId,
    ]);
    $format = Format::factory()->create([
        'media_type_id' => $this->mediaTypeId,
    ]);
    $series = Series::factory()->create();
    $fields = ['title', 'published_year', 'part'];
    [$book1, $book2] = Book::factory()->count(2)->create([
        'genre_id' => $genre->id,
        'format_id' => $format->id,
        'series_id' => $series->id,
    ]);
    get(route('books.index'))
        ->assertOk()
        ->assertSeeText([
            ...$book1->only($fields),
            ...$book2->only($fields),
            $genre->name,
            $format->name,
            $series->name,
        ]);
});

Show:

it('it shows all the information about a book', function () {
    $book = Book::factory()
        ->has(Author::factory())
        ->for($genre = Genre::factory()->create([
            'media_type_id' => $this->mediaTypeId,
        ]))
        ->for($format = Format::factory()->create([
            'media_type_id' => $this->mediaTypeId,
        ]))
        ->for($series = Series::factory()->create())
        ->for($publisher = Publisher::factory()->create())
        ->create();
    $author = $book->authors->first();

    get(route('books.show', $book))
    ->assertOk()
    ->assertSeeText([
        $book->title,
        $book->isbn,
        $book->part,
        $book->blurb,
        $book->published_year,
        $author->first_name,
        $author->last_name,
        $genre->name,
        $format->name,
        $series->name,
        $publisher->name,
    ]);
});
newbie360's avatar

@Tray2 Yup, i get your point, but if the Shop A and B details is hardcoded html, not relate to database ?

For me, i think A) is more better

Please or to participate in this conversation.