T
T
the5x2021-02-19 22:03:11
Software testing
the5x, 2021-02-19 22:03:11

What is the best approach to writing tests?

I see that many in writing tests in Laravel basically fall into two categories. These approaches are different. Which one do you use and which one do you prefer and why?

The examples are not related, just random from the internet.

The first, through creating an instance of the class and then accessing the method under test:

public function testBalanceIsInitiallyZero()
    {
        $ba = new BankAccount;
        $this->assertEquals(0, $ba->getBalance());
    }


Second option, as per laravel documentation:
public function testCreateProductWithMiddleware()
        {
                $data = [
                        'name' => "New Product",
                        'description' => "This is a product",
                        'units' => 20,
                        'price' => 10,
                        'image' => "https://images.pexels.com/photos/1000084/pexels-photo-1000084.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
                               ];

            $response = $this->json('POST', '/api/products',$data);
            $response->assertStatus(401);
            $response->assertJson(['message' => "Unauthenticated."]);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-02-19
@the5x

The first is unit tests
The second is integration tests
Both are needed for good, but integration tests allow you to catch more critical bugs (kmk)
And units allow you to tightly cover edge cases

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question