lundi 4 janvier 2021

Test Laravel Repository Pattern Database Refreshed When Call The Method

I want test the return output on repository method. But when i call the method the database suddenly refreshed. But when i try to not call the repository method and i debug the table it has record. Here is my code, am i miss something ?

use App\Repositories\UserRepository;

class ExampleTest extends TestCase
{
    protected $repository;

    protected function setUp(): void
    {
        parent::setUp();

        $this->repository = app(UserRepository::class);
    }
    
    public function it_should_return_empty_when_no_filter_found()
    {
        $this->seedingDb();

        $response = $this->repository->filter(); // When i call this the record on table return empty

        dd($response); // Result empty
    }
    
    private function seedingDb()
    {
        for ($i = 0; $i < 5; $i++) {
            User::create([
                'name'          => $this->faker->word,
                'role'          => $this->faker->word,
                'property_name' => $this->faker->word,
                'email'         => $this->faker->email,
                'phone_number'  => $this->faker->phoneNumber,
                'password'      => $this->faker->word,
                'status'        => 'active',
            ]);
        }
    }
}

User Repository:

use App\Repositories\UserRepository;

class UserRepository
{
    public function filter()
    {
        return User::whereStatus('active')->get()->toArray();
    }
}

Aucun commentaire:

Enregistrer un commentaire