[Vuejs]-Laravel: Testing controller and DB result

0👍

Use feature test instead of a unit test.

When testing using feature test, the ideal things to do is to use refresh database and have factory to create your dummy data. So your database will be empty each time a test case is running.

but when in any circumstances you can’t refresh the database, and you use normal database then I have a workaround for you.

use DB::beginTransaction and DB::rollback in the end. but be careful, any unexpected DB::commit can be disastrous. But if you sure your code is fine, then you should give it a try

with that yes, you can test it with any available assertion and check if your data is in your DB or not using assertDatabaseHas

for the assertion list try read the documentation

https://laravel.com/docs/8.x/database-testing#available-assertions

https://laravel.com/docs/8.x/http-tests#available-assertions

to test you end point create feature test and get the response of your end point like this

$response = $this->get('your end point to test')

// all of your assertion like
$response->assertSuccessful();
$this->assertDatabaseHas('table name', [data to assert]);

Leave a comment