Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 27.07 즐겨찾기 서비스 테스트 코드 작성 #237 #252

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
✅ test: 즐겨찾기 등록 API 테스트 코드 작성 #237
sieunie committed Dec 2, 2024
commit ffe39e2a83133ed02321f4767230df89b582c420
66 changes: 66 additions & 0 deletions BE/src/stock/bookmark/stock-bookmark.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Test } from '@nestjs/testing';
import { BadRequestException } from '@nestjs/common';
import { StockBookmarkRepository } from './stock-bookmark.repository';
import { StockDetailService } from '../detail/stock-detail.service';
import { StockBookmarkService } from './stock-bookmark.service';

describe('stock bookmark test', () => {
let stockBookmarkService: StockBookmarkService;
let stockBookmarkRepository: StockBookmarkRepository;
let stockDetailService: StockDetailService;

beforeEach(async () => {
const mockStockBookmarkRepository = {
existsBy: jest.fn(),
create: jest.fn(),
insert: jest.fn(),
};
const mockStockDetailService = { getInquirePrice: jest.fn() };

const module = await Test.createTestingModule({
providers: [
StockBookmarkService,
{
provide: StockBookmarkRepository,
useValue: mockStockBookmarkRepository,
},
{
provide: StockDetailService,
useValue: mockStockDetailService,
},
],
}).compile();

stockBookmarkService = module.get(StockBookmarkService);
stockBookmarkRepository = module.get(StockBookmarkRepository);
stockDetailService = module.get(StockDetailService);
});

it('즐겨찾기에 등록되지 않은 종목에 대해 즐겨찾기 등록할 경우, DB에 즐겨찾기가 추가된다.', async () => {
jest.spyOn(stockBookmarkRepository, 'existsBy').mockResolvedValue(false);

const createMock = jest.fn();
jest
.spyOn(stockBookmarkRepository, 'create')
.mockImplementation(createMock);

const saveMock = jest.fn();
jest.spyOn(stockBookmarkRepository, 'insert').mockImplementation(saveMock);

await stockBookmarkService.registerBookmark(1, '005930');

expect(createMock).toHaveBeenCalledWith({
user_id: 1,
stock_code: '005930',
});
expect(saveMock).toHaveBeenCalled();
});

it('즐겨찾기에 이미 등록된 종목에 대해 즐겨찾기 등록할 경우, BadRequest 예외가 발생한다.', async () => {
jest.spyOn(stockBookmarkRepository, 'existsBy').mockResolvedValue(true);

await expect(
stockBookmarkService.registerBookmark(1, '005930'),
).rejects.toThrow(BadRequestException);
});
});