【发布时间】:2016-10-22 18:19:01
【问题描述】:
我在 angularJS 项目中为我的服务编写测试时遇到了一些麻烦。 我正在使用 Karma 和 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 编写的,当我启动测试时,我遇到了与我的服务相关的语法错误。
这是我的配置文件:
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 : 我已经安装了 karma-coffee-preprocessor
【问题讨论】:
-
“什么不工作?”,控制台中的任何错误?
-
您是否在测试用例配置中添加了特定文件?
-
@PankajParkar:我无法处理 coffeeScript 控制器/服务
-
@Rohit ,我更新了我的问题并放置了我的配置测试文件。你能告诉我我的做法是否正确吗
标签: angularjs unit-testing service ionic-framework karma-jasmine