0.初始化node
npm init
1.編寫package.json
{
"name": "jasmine-test",
"version": "1.0.0",
"description": "測(cè)試jasmine模組",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node app.js",
"test": "node run-tests.js"
},
"devDependencies": {
"jasmine": "^2.6.0"
},
"author": "DEXION",
"license": "ISC"
}
2.使用npm進(jìn)行安裝
npm install
3.使用npm全局安裝jasmine模塊:
npm install -g jasmine
4.初始化jasmine
jasmine init
5.在項(xiàng)目文件夾內(nèi)
- sepc文件夾內(nèi)創(chuàng)建以-spec為后綴的js測(cè)試文件
- 在js文件夾內(nèi)創(chuàng)建待測(cè)試js腳本
- 創(chuàng)建run-test.js
const Jasmine = require('jasmine');
const jasmine = new Jasmine();
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.execute();
完成以上操作 便可以書寫測(cè)試樣例了
以下是測(cè)試樣例:
- main.js
module.exports = function resolveInteger2QualityFactor(num) {
var result = '';
var i = 2;
while (num > 1) {
if (num % i === 0) {
result += i;
num /= i;
}
else {
i = i + 1;
}
}
console.log(result);
return result;
}
- main-spec.js
const resolveInteger2QualityFactor = require('../js/main');
describe('分解質(zhì)因數(shù)', () => {
const num1 = 90;
const num2 = 120;
it('1.分解90質(zhì)因數(shù)', () => {
const result = resolveInteger2QualityFactor(num1);
expect(result).toEqual('2335');
})
it('2.分解120質(zhì)因數(shù)', () => {
const result = resolveInteger2QualityFactor(num2);
expect(result).toEqual('22235');
})
})
測(cè)試樣例github地址:https://github.com/Dexter0ion/Thoughtwork-2017Summer/tree/master/tasks/muscle-battle-aug8th