說來慚愧,研究angularjs也有一段時間了,卻一直拋棄了angularjs最大的好處之一——測試。這個週末下決心研究下怎麼用給angularjs應用作測試。angularjs
#安裝karmaweb
其實,這麼久以來一直沒用到angularjs的測試就是由於不會配置karma。由於項目一開始的時候看着文檔跑了一遍,沒跑成功就放下了。這兩天有細讀了文檔,發現當初也不能全怪我笨,angularjs的文檔寫的確實操蛋。karma都更新好多了,它仍是舊的文檔、代碼,難怪我跑不通。chrome
好了,開始安裝karma。npm
第一: 打開cmd,輸入> npm install -g karma 就好了windows
第二:配置文件,這個版本(0.10)配置挺簡單的,有提示,一步步確認就好了。瀏覽器
第三:(文檔裏沒寫,從錯誤信息裏看出來的)設置環境變量 CHROME_BIN "你的chrome安裝路徑"。這一步相當重要,由於本身安裝chrome時的一些設置,karma在系統默認的一些變量裏找不到chrome瀏覽器,就會致使測試失敗。當初大概也是被絆倒在這裏的。app
剩下的就簡單了,本身在命令行裏生成一個karma配置文件替換掉angualr-phonecat裏的karma配置文件就OK了。下面是我生成的配置文件demo,親測在windows 7下OK測試
// Karma configuration // Generated on Sun Aug 18 2013 16:26:13 GMT+0800 (中國標準時間) module.exports = function(config) { config.set({ // base path, that will be used to resolve files and exclude basePath: '../', // frameworks to use frameworks: ['jasmine'], // list of files / patterns to load in the browser files: [ 'app/lib/angular/angular.js', 'app/lib/angular/angular-*.js', 'test/lib/angular/angular-mocks.js', 'app/js/**/*.js', 'test/unit/**/*.js' ], // list of files to exclude exclude: [ ], // test results reporter to use // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' 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, currently available: // - Chrome // - ChromeCanary // - Firefox // - Opera // - Safari (only Mac) // - PhantomJS // - IE (only Windows) browsers: ['Chrome'], // If browser does not capture in given timeout [ms], kill it captureTimeout: 60000, // Continuous Integration mode // if true, it capture browsers, run tests and exit singleRun: false }); };