我在一個類中有一個方法,計算回傳 Promise 的 2 個引數的總和(必須):
module.exports = class Sum {
sum(a, b) {
let c = a b;
const myPromise = new Promise(function(myResolve) {
setTimeout(function(){ myResolve(c); }, 100);
});
return myPromise;
}
}
我使用 Jasmine 框架進行單元測驗
const MyLocalVariable1 = require('../src/sum');
describe('CommonJS modules', () => {
it('should import whole module using module.exports = sum;', async () => {
const result = new MyLocalVariable1();
expect(result.sum(4, 5).then(function(value) {
return value;
})).toBe(9);
});
}
第一個運算式是我們要測驗的:
result.sum(4, 5).then(function(value) {
return value;
})
第二個是期望值:
toBe(9)
但是我怎樣才能從第一個運算式中得到一個值,因為它是一個 Promise,它的期望值是[object Promise]。提前致謝
uj5u.com熱心網友回復:
要將康拉德的觀點帶回家,您可以執行以下操作:
it('should import whole module using module.exports = sum;', async () => {
const result = new MyLocalVariable1();
const answer = await result.sum(4, 5);
expect(answer).toBe(9);
});
或以下內容:
// add done callback to tell Jasmine when you're done with the unit test
it('should import whole module using module.exports = sum;', (done) => {
const result = new MyLocalVariable1();
result.sum(4, 5).then(answer => {
expect(answer).toBe(9);
done();
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/460499.html
標籤:javascript 单元测试 茉莉花
