我在我的angularJS項目中爲我的服務編寫測試有一些麻煩。 我使用Karma和Jasmine進行單元測試。 首先,我選擇了一個沒有依賴關係的服務,但我從未通過過測試。服務/工廠單元測試 - AngularJS - Jasmine
這裏是我的服務(以書面的CoffeeScript)
angular.module('app').factory 'rankFactory', [ ->
rankFactory = {}
ranks = [
{
id: 0
label: 'RANK0'
}
{
id: 1
label: 'RANK1'
}
]
rankFactory.getRanks = ->
ranks
rankFactory.getRanks = (id) ->
ranks[id]
rankFactory
]
服務工作正常。因此,測試沒有。這是我的測試:
describe('rank Factory unit tests', function(){
describe ('when I call myService rankFactory.getRanks()', function() {
beforeEach(module('app'));
it('returns ranks', inject(function(rankFactory){
expect(rankFactory.getRanks()).not.to.equal(null);
}))
}
)
});
我一直在嘗試多個小時,並閱讀了很多問題和文檔,但仍然無法找出它爲什麼不起作用。你能幫我嗎?
---------------------------------------------- - - - - - - - - - 編輯 - - - - - - - - - - - - - - - - ----------------------------
我發現我的問題與coffeeScript有關。 我的控制器和服務是用coffeeScript編寫的,當我啓動我的測試時,我得到了與我的服務相關的語法錯誤。
這裏是我的配置文件:
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: [
'../bower_components/angular/angular.js',
'../bower_components/angular-ui-router/release/angular-ui-router.js',
'../bower_components/angular-mocks/angular-mocks.js',
'../src/scripts/**/*.coffee',
'../src/scripts/Services/rankService.coffee',
'unit-tests/**/*.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'**/*.coffee': ['coffee']
},
coffeePreprocessor: {
// options passed to the coffee compiler
options: {
bare: true,
sourceMap: false
},
// transforming the filenames
transformPath: function(path) {
return path.replace(/\.coffee$/, '.js')
}
},
// 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_DEBUG,
// 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: ['PhantomJS'],
// 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
})
}
我寫我在JavaScript測試中,我很困惑什麼我必須做,使之覆蓋CoffeeScript的。
PS:我已經安裝了卡瑪 - 咖啡預處理
「什麼不工作?」,在控制檯中的任何錯誤? –
您是否在測試用例配置中添加了特定文件? – Rohit
@PankajParkar:我無法處理coffeeScript控制器/服務 – AsmaG