I'm trying to write unit test for my application. I'm using repository pattern with service. This is my code: AdminService.php
class AdminService
{
protected $adminRepository;
public function __construct(AdminRepository $adminRepository)
{
$this->adminRepository = $adminRepository;
}
public function create(Array $data)
{
$data['password'] = Hash::make($adminData['password']);
$this->adminRepository->create($adminData);
}
}
AdminService.test
class AdminServiceTest extends TestCase
{
public function testCreate()
{
$password = 'password';
$data = [
'name' => 'admin',
'email' => 'admin@gmail.com',
'password' => 'password',
];
$mock = Mockery::mock(AdminRepository::class);
$mock->shouldReceive('create')->with('data')->once();
$this->app->instance(AdminRepository::class, $mock);
$adminService = $this->app->make(AdminRepository::class);
$adminService->create($data);
$data['password'] = Hash::make($password);
$this->assertDatabaseCount('admins', 1);
$this->assertDatabaseHas('admins', $data);
}
}
But when I run test, it show an error
1) Tests\Unit\Services\AdminServiceTest::testCreate
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_2_App_Repositories_AdminRepository::create(['name' => 'admin', 'email' => 'admin@gmail.com', 'password' => 'password']). Either the method was unexpected or its arguments matched no expected argument list for this method
Then I try changing with arguments from
$mock->shouldReceive('create')->with('data')->once();
to
$mock->shouldReceive('create')->with($data)->once();
It worked without error but result returned fail.
Failed asserting that table [admins] matches expected entries count of 1. Entries found: 0.
How do I correct this?
Aucun commentaire:
Enregistrer un commentaire