' P '

whatever I will forget

Certified Javascript Developer Testing Section

Date.prototype.getMonth()

  • 返却値は数値。そして0始まりとなるので、JanuaryのDate型が渡された場合は 0が返されるので注意.
const moonLanding = new Date('January 20, 69 00:20:18');

console.log(moonLanding.getMonth()); // (January gives 0)

developer.mozilla.org


False Negative

  • コード(ロジック)は問題ないのに、テストロジックがおかしいせいでテストが失敗してしまうこと

  • 例えば、下記の例の上部を見てみる:

function getWeekEarlier(date) {
  return new Date(date - (7 * 24 * 60 * 60 * 1000));
};

// False Negative 部分
console.log(getWeekEarlier(new Date('October 10, 2020')));
console.log(new Date('October 3, 2020'));
console.log(getWeekEarlier(new Date('October 10, 2020')) === new Date('October 3, 2020')); // これはEqualにならない

// こちらが正しい
console.log(getWeekEarlier(new Date('October 10, 2020')).valueOf());
console.log(new Date('October 3, 2020').valueOf());
console.log(getWeekEarlier(new Date('October 10, 2020')).valueOf() === new Date('October 3, 2020').valueOf());
Sat Oct 03 2020 00:00:00 GMT+0900 (Japan Standard Time)
Sat Oct 03 2020 00:00:00 GMT+0900 (Japan Standard Time)
false
1601650800000
1601650800000
true
  • Date型の比較の際には、 valueOf()を使用しよう!!

kentcdodds.com

False Positive

  • 偽陽性(false positive)は、テスト結果が正しくても、コード(ロジック)に問題が存在する場合に発生します.
    • 単体テストで全てのケースを実施しきれていない場合に発生.
function getMonthEarlier(date) { // Receives a date object and returns a date object that is one month earlier.
  return new Date(date.setMonth(date.getMonth() === 1 ? 11 : date.getMonth() - 1));
// 正しくはこちら
// return new Date(date.setMonth(date.getMonth() === 1 ? 0 : date.getMonth() - 1));
};

console.log(getMonthEarlier(new Date('October 10, 2020')).getMonth()); // 8, Assertion OK
console.log(getMonthEarlier(new Date('January 10, 2020')).getMonth()); // 11, Assertion OK
// このテストがないとコードに問題があることに気がつけない
console.log(getMonthEarlier(new Date('February 10, 2020')).getMonth()); // 11, Assertion NG

isNaN()

  • Not a Numberの略.
  • 下記の文字列がある:
let str = "This is string but includes some number like 1 2 3 4"
console.log(str.split(" ").filter(elem => !isNaN(elem)));
// (4) ["1", "2", "3", "4"]
  • 1, 2, 3, 4がちゃんと抜き出される.
  • "1, 2, 3, 4" だと 4しか抜き出されない. "1. 2. 3. 4" だと問題ない... そこらへんの謎は今は一旦放置する.

developer.mozilla.org


Test Concept

Mocking: テストにおいて、関数の動作を評価・検証するために、依存関係をオブジェクトに置き換えるために使用されます。
Stubbing: 実際の出力の代わりにあらかじめ決められた値を返すことです。
False Positive: ユニットテストの内部ロジックに欠陥があったために、テストが成功したことを意味します。
Black Box Testing: テストがエンドユーザーの視点からアプリケーションの動作に焦点を当てるテストアプローチを説明するために使用されます。

Black Box / White Box Tesing

ブラックボックステストの特徴:
  1. 主にソフトウェアテスターが行う。
  2. 内部コードの知識は必要ない。
  3. 主に機能性に重点を置く。
  4. 通常、ソフトウェア要件に基づく。
  5. 通常、あまり時間がかかりません。
ホワイトボックステストの特徴:
  1. ソフトウェアプログラマーが担当することが多い。
  2. 内部コードやプロセスに関する知識が必要です。
  3. 主にソフトウェアの構造とその実装方法に焦点を当てる。
  4. 通常、設計書に基づいて行われます。
  5. 通常、より多くの時間を要します。