Ich habe einige Probleme beim Schreiben von Tests für meine Dienste in meinem angularJS-Projekt. Ich verwende Karma und Jasmin für meine Komponententests. Um zu beginnen, wählte ich einen Dienst ohne Abhängigkeiten, aber ich hatte nie die Tests bestanden.Komponententests von Dienstleistungen/Fabriken - AngularJS - Jasmine
Hier ist mein Service (geschrieben mit 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
]
Der Service funktioniert gut. Also, der Test nicht. Hier ist mein Test:
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);
}))
}
)
});
ich für mehrere Stunden versucht habe, und eine Menge von Fragen und Dokumentation lesen, aber immer noch nicht herausfinden, warum es nicht funktioniert. Kannst du mir bitte helfen?
---------------------------------------------- ------------------BEARBEITEN------------------------------- ----------------------------
Ich fand heraus, dass mein Problem mit CoffeeScript verwandt ist. Meine Controller, Dienste werden mit coffeeScript geschrieben und wenn ich meine Tests starte, habe ich Syntaxfehler in Bezug auf meine Dienste bekommen.
Hier ist meine Config-Datei:
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
})
}
ich meine Tests in javaScript schreibe, und ich bin verwirrt, was ich tun muss, um es zu machen Coffee zu decken.
Ps: Ich habe Karma-Kaffee-Präprozessor
"Was funktioniert nicht?", Irgendwelche Fehler in der Konsole? –
haben Sie die bestimmte Datei in Ihrer Testfallkonfiguration hinzugefügt? – Rohit
@PankajParkar: Ich kann nicht umgehen mit KaffeeScript Controller/Dienste – AsmaG