日渐感觉测试的重要性,不仅保证产品的质量,对以后重构提供保障
Karma的安装就不说了
npm
install -g
karma-cli
npm
install -g
karma
配置文件应放在根目录下
1 module.exports = function (config) { 2 config.set({ 3 4 // base path that will be used to resolve all patterns (eg. files, exclude) 5 basePath: ‘‘, 6 7 8 // frameworks to use 9 // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 10 frameworks: [‘jasmine‘], 11 12 13 // list of files / patterns to load in the browser 14 files: [ 15 ‘js/plugins/angular/angular.js‘, 16 ‘js/plugins/angular/angular-*.js‘, 17 ‘test/angular-mocks.js‘, 18 //recaptcha_ajax.js‘, 19 ‘js/apps/*.js‘, 20 ‘js/controllers/*.js‘, 21 ‘test/**/*.js‘ 22 ], 23 24 25 // list of files to exclude 26 exclude: [], 27 28 29 // preprocess matching files before serving them to the browser 30 // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 31 preprocessors: {}, 32 33 34 // test results reporter to use 35 // possible values: ‘dots‘, ‘progress‘ 36 // available reporters: https://npmjs.org/browse/keyword/karma-reporter 37 reporters: [‘progress‘], 38 39 40 // web server port 41 port: 9876, 42 43 44 // enable / disable colors in the output (reporters and logs) 45 colors: true, 46 47 48 // level of logging 49 // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 50 logLevel: config.LOG_INFO, 51 52 53 // enable / disable watching file and executing tests whenever any file changes 54 autoWatch: true, 55 56 57 // start these browsers 58 // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 59 browsers: [‘Chrome‘], 60 61 // 用到的插件,比如chrome浏览器与jasmine插件 62 plugins : [ 63 //‘karma-phantomjs-launcher‘, 64 ‘karma-chrome-launcher‘, 65 ‘karma-firefox-launcher‘, 66 ‘karma-jasmine‘, 67 ‘karma-junit-reporter‘ 68 ], 69 // 设置输出测试内容文件的信息 70 junitReporter : { 71 outputFile: ‘test_out/unit.xml‘, 72 suite: ‘unit‘ 73 }, 74 // Continuous Integration mode 75 // if true, Karma captures browsers, runs the tests and exits 76 singleRun: false 77 }); 78 };
实例
describe("A spec (with setup and tear-down)", function() { var foo; beforeEach(function() { foo = 0; foo += 1; }); afterEach(function() { foo = 0; }); it("is just a function, so it can contain any code", function() { expect(foo).toEqual(1); }); it("can have more than one expectation", function() { expect(foo).toEqual(1); expect(true).toEqual(true); }); });
运行
karma start karma.config.js
这篇文章可以看看:http://www.oschina.net/translate/how-to-unit-test-controllers-in-angularjs-without-setting-your-hair-on-fire
时间: 2024-09-29 08:20:01