πŸ€– 쒋은 ν…ŒμŠ€νŠΈ μ½”λ“œ | CORRECT 원칙

리버 riverΒ·2023λ…„ 11μ›” 1일
0
post-thumbnail

1. CORRECT μ›μΉ™μ΄λž€ ?


CORRECTλŠ” ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λ₯Ό μž‘μ„±ν•  λ•Œ κ³ λ €ν•΄μ•Ό ν•  λ‹€μ–‘ν•œ 상황듀을 묢어놓은 체크리슀트

C - Conformance (μ€€μˆ˜)
O - Ordering (μˆœμ„œ)
R - Range (λ²”μœ„)
R - Reference (μ°Έμ‘°)
E - Existence (쑴재)
C - Cardinality (개수)
T - Time (μ‹œκ°„)


2. μ˜ˆμ‹œμ™€ μ„€λͺ…

1) C - Conformance (μ€€μˆ˜)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” ν‘œμ€€, ν˜•μ‹, λͺ…μ„Έ 등을 잘 λ”°λ₯΄κ³  μžˆλŠ”μ§€ ν™•μΈν•œλ‹€. 예λ₯Ό λ“€μ–΄, 이메일 μ£Όμ†Œλ‚˜ μ „ν™”λ²ˆν˜Έ ν˜•μ‹μ„ μ œλŒ€λ‘œ μ€€μˆ˜ν•˜λŠ”μ§€ ν…ŒμŠ€νŠΈν•œλ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const { validatePhoneNumber } = require('./phoneValidation');

describe('Phone Number Validation Tests', () => {
  test('should conform to phone number format', () => {
    expect(validatePhoneNumber('+12345678900')).toBeTruthy();
    expect(validatePhoneNumber('invalid-phone')).toBeFalsy();
  });
});

// phoneValidation.js
function validatePhoneNumber(phone) {
  const re = /^\+\d{11}$/;
  return re.test(phone);
}

module.exports = { validatePhoneNumber };
  • 이 μ½”λ“œλŠ” μ „ν™”λ²ˆν˜Έ ν˜•μ‹μ„ μ œλŒ€λ‘œ μ€€μˆ˜ν•˜λŠ”μ§€ ν™•μΈν•˜λŠ” validatePhoneNumber ν•¨μˆ˜μ— λŒ€ν•œ ν…ŒμŠ€νŠΈλ₯Ό 보여쀀닀. μ˜¬λ°”λ₯Έ ν˜•μ‹μ˜ μ „ν™”λ²ˆν˜Έ ('+12345678900')와 잘λͺ»λœ ν˜•μ‹ ('invalid-phone')에 λŒ€ν•œ 검증을 μˆ˜ν–‰ν•¨μœΌλ‘œμ¨, μ „ν™”λ²ˆν˜Έμ˜ μ •ν™•ν•œ ν˜•μ‹μ„ μ˜¬λ°”λ₯΄κ²Œ μ€€μˆ˜ν•˜λŠ”μ§€ ν™•μΈν•œλ‹€. 이처럼 λͺ…세와 ν‘œμ€€μ„ μ€€μˆ˜ν•˜λŠ” ν…ŒμŠ€νŠΈλŠ” μ†Œν”„νŠΈμ›¨μ–΄κ°€ μ™ΈλΆ€ μ‹œμŠ€ν…œκ³Ό 잘 톡합될 수 μžˆλ„λ‘ 보μž₯ν•œλ‹€.

5) O - Ordering (μˆœμ„œ)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” λ°μ΄ν„°λ‚˜ 이벀트의 μˆœμ„œκ°€ μ€‘μš”ν•  λ•Œ κ·Έ μˆœμ„œλ₯Ό 잘 지킀고 μžˆλŠ”μ§€ ν™•μΈν•œλ‹€. 예λ₯Ό λ“€μ–΄, 거래 내역이 μ˜¬λ°”λ₯Έ μˆœμ„œλŒ€λ‘œ μ €μž₯λ˜λŠ”μ§€, ν”„λ‘œμ„ΈμŠ€ 단계가 μ˜¬λ°”λ₯Έ μˆœμ„œλ‘œ μ§„ν–‰λ˜λŠ”μ§€ 등을 μ‚΄ν•€λ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const { processSteps } = require('./processManager');

describe('Process Ordering Tests', () => {
  test('should follow the correct order of process steps', () => {
    const stepsOrder = processSteps();
    expect(stepsOrder).toEqual(['Step1', 'Step2', 'Step3']);
  });
});

// processManager.js
function processSteps() {
  // ν”„λ‘œμ„ΈμŠ€ 단계 μ‹€ν–‰ 둜직
  return ['Step1', 'Step2', 'Step3'];  // μ˜ˆμ‹œλ₯Ό μœ„ν•œ λ‹¨μˆœν™”λœ μˆœμ„œ
}

module.exports = { processSteps };
  • 이 μ½”λ“œλŠ” ν”„λ‘œμ„ΈμŠ€μ˜ 단계가 μ˜¬λ°”λ₯Έ μˆœμ„œλŒ€λ‘œ μ§„ν–‰λ˜λŠ”μ§€ ν™•μΈν•˜λŠ” processSteps ν•¨μˆ˜μ— λŒ€ν•œ ν…ŒμŠ€νŠΈλ₯Ό λ‚˜νƒ€λ‚Έλ‹€. 'Step1', 'Step2', 'Step3' μˆœμ„œλ‘œ μ§„ν–‰λ˜λŠ”μ§€ κ²€μ¦ν•¨μœΌλ‘œμ¨, ν”„λ‘œμ„ΈμŠ€μ˜ μˆœμ„œκ°€ μ˜¬λ°”λ₯΄κ²Œ μœ μ§€λ˜κ³  μžˆλŠ”μ§€ ν™•μΈν•œλ‹€. μ΄λŸ¬ν•œ μˆœμ„œμ  정확성을 κ²€μ¦ν•˜λŠ” ν…ŒμŠ€νŠΈλŠ” λ³΅μž‘ν•œ μ›Œν¬ν”Œλ‘œμš°μ™€ ν”„λ‘œμ„ΈμŠ€κ°€ μ˜¬λ°”λ₯΄κ²Œ κ΄€λ¦¬λ˜κ³  μžˆμŒμ„ 보μž₯ν•œλ‹€.

6) R - Range (λ²”μœ„)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” μž…λ ₯μ΄λ‚˜ 좜λ ₯ κ°’μ˜ λ²”μœ„λ₯Ό κ²€μ‚¬ν•œλ‹€. 예λ₯Ό λ“€μ–΄, λ‚˜μ΄ μž…λ ₯ ν•„λ“œκ°€ 0μ—μ„œ 120 μ‚¬μ΄μ˜ 값을 λ°›λŠ”μ§€, 계산 κ²°κ³Όκ°€ νŠΉμ • λ²”μœ„ 내에 μžˆλŠ”μ§€ 등을 ν™•μΈν•œλ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const { calculateInterest } = require('./financialCalculator');

describe('Interest Calculation Range Tests', () => {
  test('should return interest within valid range', () => {
    const interest = calculateInterest(1000, 5);
    expect(interest).toBeGreaterThanOrEqual(50);
    expect(interest).toBeLessThanOrEqual(150);
  });
});

// financialCalculator.js
function calculateInterest(principal, rate) {
  // 볡리 계산을 μœ„ν•œ κ°„λ‹¨ν•œ 둜직 μ˜ˆμ‹œ
  return principal * (rate / 100);
}

module.exports = { calculateInterest };
  • 이 μ½”λ“œλŠ” 금육 κ³„μ‚°μ˜ κ²°κ³Όκ°€ νŠΉμ • λ²”μœ„ 내에 μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” calculateInterest ν•¨μˆ˜μ— λŒ€ν•œ ν…ŒμŠ€νŠΈλ₯Ό λ‚˜νƒ€λ‚Έλ‹€. μ˜ˆκΈˆμ•‘ 1000에 λŒ€ν•΄ 이자율 5%κ°€ μ μš©λ˜μ—ˆμ„ λ•Œ, κ³„μ‚°λœ μ΄μžκ°€ 50 이상 150 μ΄ν•˜μΈμ§€ κ²€μ¦ν•¨μœΌλ‘œμ¨, 이자 계산이 합리적인 λ²”μœ„ λ‚΄μ—μ„œ 이루어지고 μžˆλŠ”μ§€ ν™•μΈν•œλ‹€. 이처럼 λ²”μœ„λ₯Ό κ²€μ¦ν•˜λŠ” ν…ŒμŠ€νŠΈλŠ” μž…λ ₯κ³Ό 좜λ ₯ 값이 μ˜ˆμƒ κ°€λŠ₯ν•œ λ²”μœ„ λ‚΄μ—μ„œ μ •ν™•ν•˜κ²Œ 처리되고 μžˆμŒμ„ 보μž₯ν•œλ‹€.

3) R - Reference (μ°Έμ‘°)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” μ™ΈλΆ€ μ‹œμŠ€ν…œμ΄λ‚˜ λͺ¨λ“ˆμ— λŒ€ν•œ μ°Έμ‘°κ°€ μ˜¬λ°”λ₯΄κ²Œ 이루어지고 μžˆλŠ”μ§€ ν™•μΈν•œλ‹€. 예λ₯Ό λ“€μ–΄, λ°μ΄ν„°λ² μ΄μŠ€ μ—°κ²°, 파일 μ‹œμŠ€ν…œ μ ‘κ·Ό λ“± μ™ΈλΆ€ μžμ›μ˜ μ°Έμ‘°λ₯Ό ν™•μΈν•œλ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const { readConfig } = require('./configReader');

describe('Config File Reference Tests', () => {
  test('should correctly reference and read the config file', () => {
    const config = readConfig('/path/to/config.json');
    expect(config).toHaveProperty('databaseUrl');
  });
});

// configReader.js
const fs = require('fs');

function readConfig(filePath) {
  try {
    const rawData = fs.readFileSync(filePath);
    return JSON.parse(rawData);
  } catch (error) {
    throw new Error('Unable to read config file');
  }
}

module.exports = { readConfig };
  • 이 μ½”λ“œλŠ” μ™ΈλΆ€μ˜ JSON ꡬ성 νŒŒμΌμ„ μ°Έμ‘°ν•˜μ—¬ ν•„μš”ν•œ 데이터λ₯Ό μ˜¬λ°”λ₯΄κ²Œ μ½μ–΄μ˜€λŠ”μ§€ κ²€μ‚¬ν•˜λŠ” readConfig ν•¨μˆ˜μ— λŒ€ν•œ ν…ŒμŠ€νŠΈλ₯Ό 보여쀀닀. 특히, ꡬ성 파일이 databaseUrl 속성을 ν¬ν•¨ν•˜κ³  μžˆλŠ”μ§€ ν™•μΈν•¨μœΌλ‘œμ¨, μ™ΈλΆ€ νŒŒμΌμ— λŒ€ν•œ 참쑰와 데이터 읽기 과정이 μ •μƒμ μœΌλ‘œ 이루어지고 μžˆλŠ”μ§€ κ²€μ¦ν•œλ‹€. 이처럼 μ°Έμ‘°λ₯Ό κ²€μ¦ν•˜λŠ” ν…ŒμŠ€νŠΈλŠ” μ†Œν”„νŠΈμ›¨μ–΄κ°€ μ™ΈλΆ€ μžμ›μ„ μ •ν™•ν•˜κ²Œ 닀루고 μžˆμŒμ„ ν™•μΈν•œλ‹€.

4) E - Existence (쑴재)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” ν•„μš”ν•œ κ°’, λ³€μˆ˜, 파일, 데이터 등이 μ‹€μ œλ‘œ μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•œλ‹€. 예λ₯Ό λ“€μ–΄, ν•„μš”ν•œ 파일이 디렉토리에 μ‘΄μž¬ν•˜λŠ”μ§€, ν•„μˆ˜ 데이터 ν•„λ“œκ°€ λΉ„μ–΄ μžˆμ§€ μ•Šμ€μ§€ 등을 κ²€μ‚¬ν•œλ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const fs = require('fs');
const { expect } = require('chai');

describe('File Existence Tests', () => {
  it('should confirm that the required file exists', () => {
    const filePath = './data/users.json';
    const fileExists = fs.existsSync(filePath);
    expect(fileExists).to.be.true;
  });
});

// data/users.json
// {
//   "users": [...]
// }
  • 이 μ½”λ“œλŠ” νŠΉμ • κ²½λ‘œμ— ν•„μš”ν•œ users.json 파일이 μ‹€μ œλ‘œ μ‘΄μž¬ν•˜λŠ”μ§€λ₯Ό κ²€μ‚¬ν•œλ‹€. 파일의 쑴재 μ—¬λΆ€λ₯Ό ν™•μΈν•¨μœΌλ‘œμ¨, μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ ν•„μš”ν•œ 데이터에 μ ‘κ·Όν•  수 μžˆλŠ”μ§€ κ²€μ¦ν•œλ‹€. μ‘΄μž¬μ„±μ„ ν™•μΈν•˜λŠ” ν…ŒμŠ€νŠΈλŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ μ•ˆμ •μ„±κ³Ό 데이터 무결성을 보μž₯ν•˜λŠ” 데 μ€‘μš”ν•œ 역할을 ν•œλ‹€.

5) C - Cardinality (개수)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” μ½œλ ‰μ…˜μ΄λ‚˜ κ·Έλ£Ή λ‚΄μ˜ μš”μ†Œ κ°œμˆ˜κ°€ μ •ν™•ν•˜κ³  μ μ ˆν•œμ§€ ν™•μΈν•œλ‹€. 예λ₯Ό λ“€μ–΄, μ‚¬μš©μž λͺ©λ‘μ—μ„œ 100λͺ…μ˜ μ‚¬μš©μžκ°€ 쀑볡 없이 μžˆλŠ”μ§€ 등을 κ²€μ¦ν•œλ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const { getUserCount } = require('./userManagement');

describe('User Count Validation Tests', () => {
  test('should have correct number of users without duplication', () => {
    const users = [
      { id: 1, name: "Alice" },
      { id: 2, name: "Bob" },
      // ... 기타 μ‚¬μš©μž
      { id: 100, name: "Zoe" }
    ];
    expect(getUserCount(users)).toBe(100);
  });
});

// userManagement.js
function getUserCount(users) {
  const uniqueUsers = new Set(users.map(user => user.id));
  return uniqueUsers.size;
}

module.exports = { getUserCount };
  • 이 μ½”λ“œλŠ” μ‚¬μš©μž λͺ©λ‘μ—μ„œ 쀑볡 없이 100λͺ…μ˜ μ‚¬μš©μžκ°€ μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” getUserCount ν•¨μˆ˜μ— λŒ€ν•œ ν…ŒμŠ€νŠΈλ₯Ό 보여쀀닀. Set 객체λ₯Ό μ‚¬μš©ν•˜μ—¬ μ€‘λ³΅λœ μ‚¬μš©μž IDλ₯Ό μ œκ±°ν•˜κ³ , 결과적으둜 μœ μΌν•œ μ‚¬μš©μžμ˜ 수λ₯Ό λ°˜ν™˜ν•œλ‹€. 이런 μ‹μœΌλ‘œ μš”μ†Œμ˜ 개수λ₯Ό μ •ν™•ν•˜κ²Œ κ΄€λ¦¬ν•˜λŠ” ν…ŒμŠ€νŠΈλŠ” λ°μ΄ν„°μ˜ μ •ν™•μ„±κ³Ό 신뒰성을 보μž₯ν•œλ‹€.

6) T - Time (μ‹œκ°„)


πŸ’¬Β Β μƒν™©

ν…ŒμŠ€νŠΈλŠ” 처리 μ‹œκ°„, 응닡 μ‹œκ°„, 이벀트 μˆœμ„œ λ“± μ‹œκ°„κ³Ό κ΄€λ ¨λœ 속성듀이 κΈ°λŒ€μΉ˜λ₯Ό λ§Œμ‘±ν•˜λŠ”μ§€ ν™•μΈν•œλ‹€. 예λ₯Ό λ“€μ–΄, 데이터 μ²˜λ¦¬μ— μ†Œμš”λ˜λŠ” μ‹œκ°„μ΄λ‚˜ μΊμ‹œ 만료 μ‹œκ°„μ„ κ²€μ¦ν•œλ‹€.

πŸ’»Β Β μ˜ˆμ‹œ μ½”λ“œ

const { processData } = require('./dataProcessor');

describe('Data Processing Time Tests', () => {
  test('should process data within acceptable time limit', () => {
    const startTime = performance.now();
    processData(largeDataSet);
    const endTime = performance.now();
    const processingTime = endTime - startTime;
    
    expect(processingTime).toBeLessThan(1000); // μ˜ˆμƒ 처리 μ‹œκ°„ 1초 미만
  });
});

// dataProcessor.js
function processData(data) {
  // λ³΅μž‘ν•œ 데이터 처리 둜직
}

module.exports = { processData };
  • 이 μ½”λ“œλŠ” λŒ€μš©λŸ‰ 데이터 μ„ΈνŠΈλ₯Ό μ²˜λ¦¬ν•˜λŠ” 데 κ±Έλ¦¬λŠ” μ‹œκ°„μ„ μΈ‘μ •ν•˜μ—¬ μ˜ˆμƒ 처리 μ‹œκ°„ 내에 μ™„λ£Œλ˜λŠ”μ§€ κ²€μ¦ν•˜λŠ” processData ν•¨μˆ˜μ— λŒ€ν•œ ν…ŒμŠ€νŠΈλ₯Ό 보여쀀닀. μ„±λŠ₯이 μ€‘μš”ν•œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œλŠ” μ΄λŸ¬ν•œ μ‹œκ°„ 기반 ν…ŒμŠ€νŠΈλ₯Ό 톡해 μ‚¬μš©μž κ²½ν—˜μ„ ν–₯μƒμ‹œν‚€κ³  μ‹œμŠ€ν…œμ˜ νš¨μœ¨μ„±μ„ 보μž₯ν•œλ‹€.
profile
ν”„λ‘ νŠΈμ—”λ“œ 개발자

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보