BoardsService Test
라라벨 테스트 assert를 통해 테스트코드 작성 실습
테스트코드를 루트\Tests\Unit 폴더에 단위테스트 코드를 작성한 후 아래의 명령어를 터미널에서 실행하면 테스트가 가능
php .\\vendor\\bin\\phpunit
Mocking을 하고싶다면 composer를 통해 설치
composer require --dev mockery/mockery
<?php
namespace Tests\\Unit;
use Tests\\TestCase;
use Illuminate\\Foundation\\Testing\\RefreshDatabase;
use Illuminate\\Support\\Facades\\DB;
use Mockery;
use App\\Http\\Services\\BoardsService;
use stdClass;
use App\\Http\\Dtos\\BoardCommentAddDto;
use App\\Models\\Comment;
use Illuminate\\Database\\Eloquent\\Model;
use Illuminate\\Support\\Collection;
class UserTest extends TestCase
{
private $boardsService;
function setUp(): void
{
parent::setUp(); // TestCase의 기본 설정을 불러오기
$this->boardsService = new BoardsService();
}
public function testShow()
{
$mock = Mockery::mock('Illuminate\\Database\\Query\\Builder');
$mock->shouldReceive('join')
->once()
->with('users', 'boards.user_id', '=', 'users.id')
->andReturnSelf(); // 메서드 체이닝을 계속하기 위해 자기 자신을 반환
$mock->shouldReceive('where')
->once()
->with('boards.id', 1)
->andReturnSelf(); // 메서드 체이닝을 계속하기 위해 자기 자신을 반환
$mock->shouldReceive('select')
->once()
->with('boards.*', 'users.name as username')
->andReturnSelf(); // 메서드 체이닝을 계속하기 위해 자기 자신을 반환
$mock->shouldReceive('first')
->once()
->andReturn((object)['id' => 1, 'username' => 'Test User', 'content' => '글입니다']); // 가짜 데이터 반환
DB::shouldReceive('table')
->once()
->with('boards')
->andReturn($mock);
$result = $this->boardsService->show(1);
// 결과 검증
$this->assertInstanceOf(stdClass::class, $result);
$this->assertEquals(1, $result->id);
$this->assertEquals('Test User', $result->username);
$this->assertEquals('글입니다', $result->content);
}
public function testCommentAdd()
{
$boardCommentAddDto = new BoardCommentAddDto('내용', 1);
$comment = $this->boardsService->commentAdd($boardCommentAddDto);
$mockComment = new Comment;
$mockComment->content = '내용';
$mockComment->board_id = 1;
$this->assertInstanceOf(Model::class, $comment);
$this->assertEquals($comment->content, $mockComment->content);
$this->assertEquals($comment->board_id, $mockComment->board_id);
}
public function testCommentsShow()
{
$mock = Mockery::mock('Illuminate\\Database\\Query\\Builder');
$mock->shouldReceive('where')
->once()
->with('board_id', 1)
->andReturnSelf();
$mock->shouldReceive('select')
->once()
->with('comments.*')
->andReturnSelf();
$mock->shouldReceive('get')
->once()
->andReturn(collect([
(object)['id' => 1, 'content' => '내용', 'board_id' => 1],
(object)['id' => 2, 'content' => '내용2', 'board_id' => 1]
]));
DB::shouldReceive('table')
->once()
->with('comments')
->andReturn($mock);
$result = $this->boardsService->commentsShow(1);
$this->assertInstanceOf(Collection::class, $result);
// $this->assertIsArray($result);
$this->assertEquals(1, $result[0]->id);
$this->assertEquals(2, $result[1]->id);
$this->assertEquals('내용', $result[0]->content);
$this->assertEquals('내용2', $result[1]->content);
$this->assertEquals(1, $result[0]->board_id);
$this->assertEquals(1, $result[1]->board_id);
}
}
한 메서드안의 assert갯수가 assertions 메서드 각각 tests에 포함
'공부 > Laravel' 카테고리의 다른 글
Pusher를 활용한 Laravel5.8 + Javascript 소켓통신 (0) | 2024.06.20 |
---|