php artisan make:test UserTest
php artisan test
In Laravel, test cases are written using the built-in testing framework called PHPUnit. Laravel provides a convenient way to write tests to verify the functionality and behavior of your application. Here's an example of a test case in Laravel:
s
use Tests\TestCase;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/');
$response->assertStatus(200);
$response->assertSee('Welcome to Laravel');
}
}
s
public function testSave() { // Create a new User object $user = new User; // Set the user's name $user->name = 'John Doe'; // Save the user $user->save(); // Assert that the user has been saved successfully $this->assertTrue($user->exists); }
In the above example, we create a test class ExampleTest
that extends the TestCase
class provided by Laravel. Inside the class, we define a test method testBasicTest()
.
The testBasicTest()
method is a basic example of a test. It makes an HTTP GET request to the root URL (/
) using the $this->get()
method. The response of the request is stored in the $response
variable.
We then use the assertion methods provided by Laravel's testing framework to verify the response. In this example, we assert that the response has a status code of 200 using $response->assertStatus(200)
. We also assert that the response body contains the text "Welcome to Laravel" using $response->assertSee('Welcome to Laravel')
.
To run this test case, you can use the php artisan test
command in the terminal. Laravel will execute all the test cases in your application and display the results.
This is just a basic example, and Laravel's testing framework offers a wide range of assertion methods and testing utilities to write more comprehensive tests for your application. You can explore the official Laravel documentation on testing for more information and examples.
s
use Tests\TestCase;
use App\Models\User;
class UserTest extends TestCase
{
/**
* Test creating a user.
*
* @return void
*/
public function testCreateUser()
{
$userData = [
'name' => 'John Doe',
'email' => 'johndoe@example.com',
'password' => bcrypt('password123'),
];
$user = User::create($userData);
$this->assertInstanceOf(User::class, $user);
$this->assertDatabaseHas('users', [
'email' => 'johndoe@example.com',
]);
}
}
s
In this example, we have a UserTest
class that extends the TestCase
class. Inside the class, we define a test method testCreateUser()
.
The testCreateUser()
method tests the creation of a user by creating a new user with some sample data using the User::create()
method. The sample data includes the user's name, email, and password (which is hashed using Laravel's bcrypt()
function).
We then use the assertInstanceOf()
method to assert that the created user instance is an instance of the User
model.
Next, we use the assertDatabaseHas()
method to assert that the user record exists in the database table users
with the specified email address.
To run this test case, you can use the php artisan test
command. Laravel will execute the test and display the results.
This example showcases how you can write tests to verify the behavior of database operations in Laravel. You can extend this example to include more comprehensive tests for CRUD operations, relationships, validations, and other aspects of your application's database interactions.
No comments:
Post a Comment