๐Ÿ’ BE TIL Day34 0427

JBยท2022๋…„ 4์›” 28์ผ
0

CodeCamp BE 02

๋ชฉ๋ก ๋ณด๊ธฐ
29/30

โฌ‡๏ธ Main Note
https://docs.google.com/document/d/1WBrt2IYSRRvwc3PvPCJ9x24PPU4T05zha4bZ06cFL0o/edit


๐ŸŒฟ TDD

File Address : aaa.spec.ts

// 1. ํ•œ๊ฐœ ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ
it('๋”ํ•˜๊ธฐ ํ…Œ์ŠคํŠธ', () => {
  // test๋ฅผ ์‹คํ–‰ํ•˜๊ณ ์‹ถ์€ ํ•จ์ˆ˜๋ฅผ ์ ์œผ๋ฉด ๋จ
  const a = 1;
  const b = 2;
  expect(a + b).toBe(3);
});

// 2. ์—ฌ๋Ÿฌ๊ฐœ ๋ฌถ์–ด์„œ ํ•œ๋ฐฉ์— ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ
describe('๋‚˜์˜ ํ…Œ์ŠคํŠธ ๊ทธ๋ฃน', () => {
  // ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ž‘์„ฑ
  it('๋”ํ•˜๊ธฐ ํ…Œ์ŠคํŠธ', () => {
    // test๋ฅผ ์‹คํ–‰ํ•˜๊ณ ์‹ถ์€ ํ•จ์ˆ˜๋ฅผ ์ ์œผ๋ฉด ๋จ
    const a = 1;
    const b = 2;
    expect(a + b).toBe(3);
  });

  it('๊ณฑํ•˜๊ธฐ ํ…Œ์ŠคํŠธ', () => {
    // test๋ฅผ ์‹คํ–‰ํ•˜๊ณ ์‹ถ์€ ํ•จ์ˆ˜๋ฅผ ์ ์œผ๋ฉด ๋จ
    const a = 3;
    const b = 4;
    expect(a * b).toBe(12);
  });
});

// 3. ์ƒํ’ˆ ๊ตฌ๋งคํ•˜๊ธฐ ํ…Œ์ŠคํŠธ ์˜ˆ์ œ
describe('์ƒํ’ˆ ๊ตฌ๋งค ํ…Œ์ŠคํŠธ', () => {
  beforeEach(() => {
    // ์ผ๋‹จ ๋กœ๊ทธ์ธ์„ ํ–ˆ๋Š”์ง€ ๊ฒ€์ฆํ•˜๋ ค๊ณ  ํ•˜๋Š”๊ฑฐ์ž„
    // ๊ทผ๋ฐ ์ด๋•Œ ๋ฌธ์ œ๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋Š” ๋ชจ๋‘ ๋…๋ฆฝ์ ์ž„
    // ๋กœ๊ทธ์ธ์ด ๊ฒ€์ฆ ๋˜์–ด์•ผ์ง€ ์•„๋ž˜ ๊ฒ€์ฆ๋“ค์ด ์‹คํ–‰์ด ๋˜๋Š”๋ฐ ์ด๊ฑด ์•ˆ๋˜๋Š”๊ฑฐ์ž„ (ํ…Œ์ŠคํŠธ๋Š” ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰์ด ๋จ ์—ฐ๊ฒฐ์ด ๋˜์–ด์žˆ์ง€ ์•Š์Œ)
    // ๊ทธ๋ž˜์„œ ์ด๋•Œ๋Š” it๋ฅผ ์“ฐ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ beforeEach๋ฅผ ์จ์ค˜์•ผํ•˜๋Š”๊ฑฐ์ž„
    //
    // ***** beforeEach๋กœ ํ•ด์คฌ์œผ๋ฉด ์ด์ œ ์ด ๋ฐ‘์— ๋กœ๊ทธ์ธ ๋กœ์ง ์ž‘์„ฑ *****
    // ๊ทธ๋Ÿฌ๋ฉด ๋ฐ‘์— ๋ˆ ๊ฒ€์ฆํ•˜๊ธฐ๋ž‘ ์ƒํ’ˆ ๊ตฌ๋งคํ•˜๊ธฐ ํ…Œ์ŠคํŠธ๋Š” ๋กœ๊ทธ์ธ ํ…Œ์ŠคํŠธ๊ฐ€ ์„ฑ๊ณตํ•ด์•ผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ์‹คํ–‰์ด ๋˜๋Š”๊ฑฐ์ž„
  });
  it('๋ˆ ๊ฒ€์ฆํ•˜๊ธฐ', () => {
    // ๋ˆ ๊ฒ€์ฆํ•˜๋Š” logic ์“ฐ๊ณ  ๊ฒฐ๊ณผ ๋ฐ˜ํ™˜
    const result = true; // ๋ˆ์ด ์ถฉ๋ถ„ํ•˜๋‹ค๊ณ  ๊ฐ€์ •
    expect(result).toBe(true);
  });

  it('์ƒํ’ˆ ๊ตฌ๋งคํ•˜๊ธฐ', () => {
    const result = true;
    expect(result).toBe(true);
  });
  // ๋งŽ์œผ๋ฉด ๋งŽ์„์ˆ˜๋ก ๊ผผ๊ผผํ•˜๊ณ  ๋””ํ…Œ์ผํ•˜๊ฒŒ ๊ฒ€์ฆ์ด ๊ฐ€๋Šฅํ•จ ๊ทผ๋ฐ ๋„ˆ๋ฌด ๋งŽ์ด ํ•˜๋ฉด ๊ทธ๊ฑฐ๋Œ€๋กœ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ์ˆ˜๋„ ์žˆ์Œ
  // ์ฒ˜์Œ์—๋Š” ๊ธฐ๋ณธ์ ์ธ ํ…Œ์ŠคํŠธ ํ•œ๋‘๊ฐœ๋งŒ ํ•ด๋‘๊ณ  ์ ์  ๋””ํ…Œ์ผํ•˜๊ฒŒ ์ถ”๊ฐ€ํ•ด ๋‚˜๊ฐ€๋Š”๊ฒŒ ์ข‹์Œ (ํ•ต์‹ฌ์€ ์ฒ˜์Œ๋ถ€ํ„ฐ ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ๋„ˆ๋ฌด ์™„๋ฒฝํ•˜๊ฒŒ ์งœ์ง€ ์•Š๋Š”๊ฒƒ)
});

File Address : aaa2.spec.ts


import { AppService } from './app.service';
import { AppController } from './app.controller';

describe('AppController', () => {
  let appController: AppController;
  let appService: AppService;

  beforeEach(() => {
    appService = new AppService();
    appController = new AppController(appService);
  });

  describe('getHello', () => {
    // describe์•ˆ์—์„œ๋Š” ์„ธ๋ถ€์ ์ธ๊ฑธ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Œ
    // app.controller์— ์žˆ๋Š” API์ค‘ getHello๋ฅผ ๊ฒ€์ฆํ•ด๋ณด์ž !!
    it('์ด ํ…Œ์ŠคํŠธ์˜ ๊ฒ€์ฆ ๊ฒฐ๊ณผ: "Hello World!"๋ฅผ ๋ฆฌํ„ดํ•ด์•ผํ•จ', () => {
      // To do so, getHello API must be executed (so first declare AppController in "beforeEach" section)
      const result = appController.getHello();
      expect(result).toBe('Hello World!');
    });
  });
});

๐Ÿงฌ Algorithm Review

Array Rotation

function rotateArray(belt, sec) {
  for (let i = 0; i < sec; i++) {
    belt.unshift(belt.pop());
  }
  return belt;
}

rotateArray([1,2,3,4,5], 3) // [ 3, 4, 5, 1, 2 ]
profile
๋‘๋น„๋‘๋ฐฅ๋ฐฅ

0๊ฐœ์˜ ๋Œ“๊ธ€