zoukankan      html  css  js  c++  java
  • 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-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 };
    View Code

     另一个requirejs

     1 // Karma configuration
     2 // Generated on Wed Oct 29 2014 19:27:14 GMT+0800 (中国标准时间)
     3 //karma start karma.config.js
     4 
     5 module.exports = function(config) {
     6     config.set({
     7 
     8         // base path that will be used to resolve all patterns (eg. files, exclude)
     9         basePath: '.',
    10 
    11 
    12         // frameworks to use
    13         // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    14         frameworks: ['jasmine'], //'requirejs',
    15 
    16 
    17         // list of files / patterns to load in the browser
    18         files: [
    19             '../../web/static/js/angularjs/1.3.0/angular.js',
    20             '../../web/static/js/angularjs/1.3.0/angular-mocks.js',
    21             '../../web/static/dist/**/*.js',
    22             {
    23                 pattern: '../../web/static/js/angularjs/1.3.0/angular-scenario.js',
    24                 included: false
    25             },
    26             'testSpec.js',
    27             'main-test.js'
    28 
    29         ],
    30 
    31 
    32         // list of files to exclude
    33         exclude: [],
    34         //'../../web/static/js/angularjs/requireMain/*.js',
    35 
    36 
    37         // preprocess matching files before serving them to the browser
    38         // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    39         preprocessors: {},
    40 
    41 
    42         // test results reporter to use
    43         // possible values: 'dots', 'progress'
    44         // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    45         reporters: ['progress'],
    46 
    47 
    48         // web server port
    49         port: 9876,
    50 
    51 
    52         // enable / disable colors in the output (reporters and logs)
    53         colors: true,
    54 
    55 
    56         // level of logging
    57         // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    58         logLevel: config.LOG_INFO,
    59 
    60 
    61         // enable / disable watching file and executing tests whenever any file changes
    62         autoWatch: true,
    63 
    64 
    65         // start these browsers
    66         // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    67         browsers: ['Chrome'],
    68 
    69         // 用到的插件,比如chrome浏览器与jasmine插件
    70         plugins: [
    71             //'karma-phantomjs-launcher',
    72             'karma-chrome-launcher',
    73             'karma-firefox-launcher',
    74             'karma-jasmine',
    75             'karma-junit-reporter'
    76         ],
    77         // 设置输出测试内容文件的信息
    78         junitReporter: {
    79             outputFile: 'test_out/unit.xml',
    80             suite: 'unit'
    81         },
    82         // Continuous Integration mode
    83         // if true, Karma captures browsers, runs the tests and exits
    84         singleRun: false
    85     });
    86 };
    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

  • 相关阅读:
    使用history.back()出现"警告: 网页已过期的解决办法"
    thinkphp5 如何将带分隔符的字符串转换成索引数组,并且遍历到前台
    MYSQL查询某字段中以逗号分隔的字符串的方法
    SpringBoot项目docker化
    全选Js
    【同步工具类】CountDownLatch
    Elasticsearch 2.3.2 安装部署
    从网络获取多张二维码图片,压缩打包下载
    传统的线程互斥技术:Synchronized关键字
    定时器的编写
  • 原文地址:https://www.cnblogs.com/yuluhuang/p/4175154.html
Copyright © 2011-2022 走看看