Answer the question
In order to leave comments, you need to log in
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());
}
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
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 questionAsk a Question
731 491 924 answers to any question