AngularJS中使用Karma单元测试初体验
■ 搭建karma测试环境
→ 创建app和test文件夹
→npm install karma --save-dev
→npm install karma-jasmine --save-dev
→npm install karma-chrome-launcher --save-dev
→karma init
→不断回车,在What is the location of your source and test files?中填写:
app/**/*.js
test/**/*.js
■ 第一个测试
→ 在test下创建test.js文件
describe("true", function(){
it("Should be true", function(){
expect(true).toBeTruthy();
});
});
→ karma start karma.confi.js
■ 安装Angular相关
→ npm install angular
→ npm install angular-mocks
→ npm install angular-resource
→ 在karma.confi.js中添加对Angular相关文件的依赖
files: [
'node_modules/angular/angular.js',
'node_modules/angular-mocks/angular-mocks.js',
'node_modules/angular-resource/angular-resource.js',
'app/**/*.js',
'test/**/*.js'
],
→ karma start karma.confi.js