當函數在類和單元測試函數計算它給我一些錯誤。如何使用TDD測試計算函數?請幫幫我。如何使用TDD單元測試此功能?
1.calculation.js
export default class calculation {
static calculate(a, b) {
var sum1 = this.addition(a, b);
var sub1 = this.subtract(a, b);
return {
sum1: sum1,
sub1: sub1
};
}
static addition (a, b) {
var sum = a + b;
return sum;
}
static subtract (a, b) {
var sub = a - b;
return sub;
}
}
2.calculation.test.js
const calculate = require('../app/calculation');
describe('calculation',() => {
let result;
beforeEach(() => {
result = calculate.calculate(100, 50);
});
it('correct sum of two number is',() => {
expect(result.sum1).toBe(150);
});
it('correct subtract of two number is',() => {
expect(result.sub1).toBe(50);
});
});
錯誤是...
●計算>兩個號碼是否正確總和
TypeError: Cannot read property 'sum1' of undefined
at Object.<anonymous> (__tests__/calculation.test.js:8:14)
at tryCallTwo (node_modules/promise/lib/core.js:45:5)
at doResolve (node_modules/promise/lib/core.js:200:13)
at new Promise (node_modules/promise/lib/core.js:66:3)
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
●計算>兩個數相減正確是
TypeError: calculate.calculate is not a function
at Object.<anonymous> (__tests__/calculation.test.js:5:18)
at tryCallTwo (node_modules/promise/lib/core.js:45:5)
at doResolve (node_modules/promise/lib/core.js:200:13)
at new Promise (node_modules/promise/lib/core.js:66:3)
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
●計算>正確的減法的兩個數字是
TypeError: Cannot read property 'sub1' of undefined
at Object.<anonymous> (__tests__/calculation.test.js:11:14)
at tryCallTwo (node_modules/promise/lib/core.js:45:5)
at doResolve (node_modules/promise/lib/core.js:200:13)
at new Promise (node_modules/promise/lib/core.js:66:3)
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
計算 ✕兩個數目正確和是(3毫秒) ✕兩個數正確減爲(1毫秒)
測試套件:1失敗,1個總 測試:2失敗,2總 快照:0總 時間:0.818s,估計1s 跑完所有測試套件。 npm ERR!測試失敗。參見上面的更多細節。
●計算>兩個號碼是否正確總和 類型錯誤:在對象不能讀取的不確定 財產‘總和’。在doResolve(node_modules/promise/lib/core.js:200:13)處,在tryCallTwo(node_modules/promise/lib/core.js:45:5) 處運行(__tests__/calculation.test.js:8:14) (node_modules/promise/lib/core.js:66:3) at tryCallOne(node_modules/promise/lib/core.js:37:12) at node_modules/promise/lib/core.js:123 :15 ●計算>正確減去兩個數字是 –
運行命令npm test然後給出這樣的錯誤。我將包括上面的所有文件。 @尼古拉斯塔。 –
更改result.sum1和result.sub1後,此處出現同樣的錯誤。 @尼古拉斯塔 –