Test Builder

이동국·2022년 12월 7일
0

이번 시간에는 test Builder 과제를 통해 처음에는 테스트 없이 바닥부터 함수를 작성해보고, console.log를 이용해 결과를 확인하는 시간을 가졌다.

detectNetwork.js

detectNetwork 함수는 어떤 카드 번호를 input으로 받아, 카드 회사의 이름('MasterCard', 'American Express)을 return 하는 함수이다.
'Diner's Club' 카드 번호는 항상 38이나 39로 시작을하고, 14 자리 숫자이고,
'American Express' 카드 번호는 항상 34 나 37로 시작하고, 15 자리 숫자이기 때문에 조건문으로 적어 주었다.

function detectNetwork(cardNumber) {
  let test = cardNumber.slice(0,2)

  if((test === '38'||'39')&&(cardNumber.length===14)){
    return `Diner's Club`;
  } if((test === '34'||'37')&&(cardNumber.length===15)){
    return 'American Express';
  }
}
    if (typeof window === "undefined") {
  module.exports = detectNetwork;
}

그 다음으로
'Visa' 카드 번호는 항상 4로 시작을하고, 14이나 16이나 19자리 숫자이고,
'MasterCard' 카드 번호는 항상 51~55로 시작하고, 16자리 숫자이기 때문에 조건문으로 적어 주었다.

function detectNetwork(cardNumber) {
  let test = cardNumber.slice(0,2)

  if((test === '38'||'39')&&(cardNumber.length===14)){
    return `Diner's Club`;
  } if((test === '34'||'37')&&(cardNumber.length===15)){
    return 'American Express';
  } if((test[0] === '4')&&(cardNumber.length=== 13 || cardNumber.length===16 || cardNumber.length===19)){
    return 'Visa';
  } if((test === '51'|| test ==='52'|| test ==='53'|| test ==='54'|| test ==='55')&&(cardNumber.length===16)){
    return 'MasterCard';
  }
}
  
  if (typeof window === "undefined") {
  module.exports = detectNetwork;
}

그리고
'Dicover' 카드 번호는 항상 6011이나 65나 644~649로 시작을하고, 16이나 19자리 숫자이기 때문에 조건문으로 적어주었다.

function detectNetwork(cardNumber) {
  let test = cardNumber.slice(0,2)
  let testDiscover = cardNumber.slice(0,4)
  let Discovers = cardNumber.slice(0,3)

  if((test === '38'||'39')&&(cardNumber.length===14)){
    return `Diner's Club`;
  } if((test === '34'||'37')&&(cardNumber.length===15)){
    return 'American Express';
  } if((test[0] === '4')&&(cardNumber.length=== 13 || cardNumber.length===16 || cardNumber.length===19)){
    return 'Visa';
  } if((test === '51'|| test ==='52'|| test ==='53'|| test ==='54'|| test ==='55')&&(cardNumber.length===16)){
    return 'MasterCard';
  }
  if((testDiscover === '6011'|| test === '65'|| Discovers >= 644 || Discovers <=649) && (cardNumber.length===16 || cardNumber.length===19)){
    return "Discover"
  }

detectNetwork.test.js

Test Builder 과제에서는 mocha라는 테스트 프레임워크와 chai라는 라이브러리를사용하였다.
그래서 test에 통과하기 위해 이렇게 작성을 해 주었고, 일부분을 수정해 주었다.
처음 해보는 테스트 빌드였지만 꽤 재미있었다.

describe("Introduction to Mocha Tests - READ ME FIRST", function() {
  

  it("오류가 발생하면 테스트가 실패합니다.", function() {
  
  });

  it("오류가 발생하지 않으므로, 실패하지 않습니다.", function() {
    
    let even = function(num) {
      return num % 2 === 0;
    };
    return even(10) === true;
  });

  
  it("예상 동작이 실제 동작과 일치하지 않을 때 오류가 발생합니다.", function() {
    let even = function(num) {
      return num % 2 === 0; 
    };

    if (even(10) !== true) {
      throw new Error("10은 짝수여야 합니다!");
    }
  });
});

describe("Diner's Club", function() {

  it("has a prefix of 38 and a length of 14", function() {
    if (detectNetwork("38345678901234") !== "Diner's Club") {
      throw new Error("Test failed");
    }
  });

  it("has a prefix of 39 and a length of 14", function() {
    if (detectNetwork("3934567890123") !== "Diner's Club") {
      
    }
  });
});

describe("American Express", function() {

  let assert = function(isTrue) {
    if (!isTrue) {
      throw new Error("Test failed");
    }
  };

  it("has a prefix of 34 and a length of 15", function() {
    assert(detectNetwork("343456789012345") === "American Express");
  });

  it("has a prefix of 37 and a length of 15", function() {
    assert(detectNetwork("373456789012345") === "American Express");
  });
});

describe("Visa", function() {

  let assert = chai.assert;

  it("has a prefix of 4 and a length of 13", function() {
    assert(detectNetwork("4123456789012") === "Visa");
  });

  it("has a prefix of 4 and a length of 16", function() {
    assert(detectNetwork("4123456789012345") === "Visa");
  });

  it("has a prefix of 4 and a length of 19", function() {
    assert(detectNetwork("4123456789012345678") === "Visa");
  });
});

describe("MasterCard", function() {
  
  let should = chai.should();

  it("has a prefix of 51 and a length of 16", function() {
    should.equal(detectNetwork("5112345678901234"), "MasterCard");
  });

  it("has a prefix of 52 and a length of 16", function() {
    should.equal(detectNetwork("5212345678901234"), "MasterCard");
  });

  it("has a prefix of 53 and a length of 16", function() {
    should.equal(detectNetwork("5312345678901234"), "MasterCard");
  });


  it("has a prefix of 54 and a length of 16", function() {
    should.equal(detectNetwork("5412345678901234"), "MasterCard");
  });

  it("has a prefix of 55 and a length of 16", function() {
    should.equal(detectNetwork("5512345678901234"), "MasterCard");
  });
});

describe("Discover", function() {
  
  let should = chai.should();

  it("has a prefix of 6011 and a length of 16", function() {
    should.equal(detectNetwork('6011123456789012'), "Discover");
  });
  it('has a prefix of 6011 and a length of 19', function() {
    should.equal(detectNetwork('6011123456789012345'), "Discover");
  });
  it('has a prefix of 65 and a length of 16', function() {
    should.equal(detectNetwork('6512345678901234'), 'Discover');
  });
  it('has a prefix of 65 and a length of 19', function() {
    should.equal(detectNetwork('6512345678901234567'), 'Discover');
  });
  for (let i = 644; i <= 649; i++) {
    it('has a prefix of ' + i + ' and a length of 16', function() {
      should.equal(detectNetwork(i.toString() + '1234567890123'), 'Discover');
    });
    it('has a prefix of ' + i + ' and a length of 19', function() {
      should.equal(detectNetwork(i.toString() + '1234567890123456'), 'Discover');
    });
  }
});

Chai는 테스트에 필요한 헬퍼 함수들이 담긴 라이브러리이다.

  • Chai는 이전에 만들었던 assert 함수와 동일한 기능을 하는 assert 함수를 제공하기 때문에 이번 과제에 유용하게 쓰였다.

  • Chai에는 Expect문법과 Should문법이 있는데 이번에는 Should문법을 사용해 보았다.

테스트 결과

다행히 잘 작동된다!

0개의 댓글