0.初始化node
npm init
1.编写package.json
{
"name": "jasmine-test",
"version": "1.0.0",
"description": "测试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进行安装
npm install
3.使用npm全局安装jasmine模块:
npm install -g jasmine
4.初始化jasmine
jasmine init
5.在项目文件夹内
- sepc文件夹内创建以-spec为后缀的js测试文件
- 在js文件夹内创建待测试js脚本
- 创建run-test.js
const Jasmine = require('jasmine');
const jasmine = new Jasmine();
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.execute();
完成以上操作 便可以书写测试样例了
以下是测试样例:
- 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('分解质因数', () => {
const num1 = 90;
const num2 = 120;
it('1.分解90质因数', () => {
const result = resolveInteger2QualityFactor(num1);
expect(result).toEqual('2335');
})
it('2.分解120质因数', () => {
const result = resolveInteger2QualityFactor(num2);
expect(result).toEqual('22235');
})
})
测试样例github地址:https://github.com/Dexter0ion/Thoughtwork-2017Summer/tree/master/tasks/muscle-battle-aug8th