Ionic 使用karma進行單元測試

1. 建立Ionic工程git

ionic start projectnameweb

cd projectnamechrome

 

2.安裝karma插件npm

npm install karma karma-jasmine karma-phantomjs-launcher --save-dev --registry=https://registry.npm.taobao.org ionic

 

3. 爲了能使用karma命令函數

1) npm install karma karma-jasmine karma-phantomjs-launcher --save-dev --registry=https://registry.npm.taobao.org工具

2) npm install --registry=https://registry.npm.taobao.org 測試

3) bower install angular-mocks --save-dev --registry=https://registry.npm.taobao.orgui

   第三條出現以下錯誤spa

  bower  ENOGIT git is not installed or not in the PATH

  解決方法:

  安裝Git,並配置Git環境變量。 或者使用Git命令行工具進入projectname路徑下執行

 

4. 建立測試文件夾

mkdir tests

cd tests

 

5. 初始化karma的配置文件

karma init my.conf.js

修改my.conf.js 文件

 files: [
      '../www/build/polyfills.js',
      '../www/build/vendor.js',
      '../www/build/main.js',
      '../www/build/0.js',
      '**/*tests.js'
    ],
完整的配置
// Karma configuration
// Generated on Sun Jan 28 2018 20:59:56 GMT+0800 (中國標準時間)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      '../www/build/polyfills.js',
      '../www/build/vendor.js',
      '../www/build/main.js',
      '../www/build/0.js',
      '**/*tests.js'
    ],


    // list of files / patterns to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

 

6. 在tests目錄下建立Controller文件夾

建立controllers.tests.js

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 () {
    //except 括號中是須要測試的函數,或是變量
    //toEqual括號中是指望的函數防止或者變量
    expect(foo).toEqual(1);
  });
  //測試用例--單個測試用例中,能夠增長多個測試項
  it("can have more than one expectation", function () {
    expect(foo).toEqual(1);
    expect(true).toEqual(true);
  });
  
});

  

7. 運行測試

karma start tests/my.conf.js

出現錯誤:

Cannot load browser "Chrome": it is not registered! Perhaps you are missing some plugin?

解決方法:

npm install karma-chrome-launcher --registry=https://registry.npm.taobao.org

 

 

8. 返回測試結果

相關文章
相關標籤/搜索