首页 > 代码库 > angularjs test

angularjs test

日渐感觉测试的重要性,不仅保证产品的质量,对以后重构提供保障

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-adapter10         frameworks: [‘jasmine‘],11 12 13         // list of files / patterns to load in the browser14         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 exclude26         exclude: [],27 28 29         // preprocess matching files before serving them to the browser30         // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor31         preprocessors: {},32 33 34         // test results reporter to use35         // possible values: ‘dots‘, ‘progress‘36         // available reporters: https://npmjs.org/browse/keyword/karma-reporter37         reporters: [‘progress‘],38 39 40         // web server port41         port: 9876,42 43 44         // enable / disable colors in the output (reporters and logs)45         colors: true,46 47 48         // level of logging49         // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG50         logLevel: config.LOG_INFO,51 52 53         // enable / disable watching file and executing tests whenever any file changes54         autoWatch: true,55 56 57         // start these browsers58         // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher59         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 mode75         // if true, Karma captures browsers, runs the tests and exits76         singleRun: false77     });78 };
View Code

 

实例

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

angularjs test