I made the following controller:
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use App\Models\Record;
class SpyController extends BaseController
{
public function add(Request $request)
{
$model = new Record();
$model->value = $request->get('value');
return new JsonResponse(['message'=>'Insert Success','record'=>$model],201);
}
}
And I want to test that upon a successful insertion, a value will be inserted:
namespace Tests\Controller;
use Illuminate\Testing\Fluent\AssertableJson;
use Laravel\Sanctum\Sanctum;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\DatabaseTestCase;
class SpyRoutesTest extends TestCase
{
public function addSuccess()
{
DB::fake()
$response = $this->put('/record',['value'=>'lalalala']);
$this->assertStatus(201);
}
}
But in my case, how I can mock the actual Insertion of the record? Will the fake() just assume a successfull insertion? Also, how I can mock an error in the database for example a connection timeout as well?