karma、jasmine作angularjs單元測試

引用文:karma、jasmine作angularjs單元測試
karma和jasmine介紹html

<1>技術介紹

karma

karma是Testacular的新名字
karma是用來自動化分析統計單元測試的node

jasmine

jasmine是作js單元測試的git

karma-coverage

<2>插件安裝

1. node.js

2. 安裝karma

npm install -g karma
npm install -g karma-cli (簡化karma調用的插件)angularjs

3. 安裝jasmine

npm install -g jasminegithub

安裝完成後出現兩個文件 jasmine/jasmine-corechrome

4. 安裝karma-coverage

npm install -g karma-coverage (測試代碼覆蓋率的插件) ( 安裝這個插件先需安裝Istanbul,不知道爲何,反正安裝了Istanbul,就能夠安裝了 )npm

<3>開始第一個項目

1. 項目的目錄結構:

2. add.js文件

function add(a,b){
    return a + b;
}

3. add-test.js

describe("my great and huge math lib",function(){
    it("should perfectly complete complex addition",function(){
        var result = add(3,5);
        expect(result).toEqual(8);
    })
})

4. conf 文件

在karma-my-proc目錄下ide

karma init pro-conf.js單元測試

5. 啓動程序

karma start pro-conf.js測試

根據提示,選擇js目錄、chrome、jasmine等

6. 也能夠編輯conf.js 文件

  1. .files
// list of files / patterns to load in the browser
    files: [
      'js/**/*.js',
      'test/**/*.js'
    ],
  1. .coverage
// test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress','coverage'],
    preprocessors : {'js/**/*.js': 'coverage'},
    coverageReporter: {
        type : 'html',
        dir : 'coverage/'
    },

<4>運行結果

<5>插件解釋--karma-coverage

引用文Karma: 3 - 測試覆蓋率
karma 的插件 karma-coverage 提供了測試代碼覆蓋率的支持。
karma 的頁面:http://karma-runner.github.io/0.8/config/coverage.html 進行了說明。karma 使用 Istanbul.實現覆蓋率統計。

首先你須要安裝這個 Karma 插件,而後須要在配置文件的三個地方進行配置。

疑難問題

組件

plugins: [
      'karma-coverage',
      'karma-jasmine',
      'karma-chrome-launcher',
      'karma-firefox-launcher'
  ]

錯誤

/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:9
      throw error('No provider for "' + name + '"!');
      ^

Error: No provider for "framework:jasmine"! (Resolving: framework:jasmine)
    at error (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:22:12)
    at Object.parent.get (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:9:13)
    at get (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:54:19)
    at /usr/local/lib/node_modules/karma/lib/server.js:138:20
    at Array.forEach (native)
    at Server._start (/usr/local/lib/node_modules/karma/lib/server.js:137:21)
    at invoke (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)
    at Server.start (/usr/local/lib/node_modules/karma/lib/server.js:102:18)
    at Object.exports.run (/usr/local/lib/node_modules/karma/lib/cli.js:243:26)
    at Object.<anonymous> (/usr/local/lib/node_modules/karma/bin/karma:3:23)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:134:18)
相關文章
相關標籤/搜索