我是新手,有測試經驗。我開發了一個Angular應用程序,希望添加測試來掌握Jasmine和Karma。所以我設置了Karma,在我的Angular應用程序中爲我的一個服務添加了一個簡單的「getGreeting」函數,並在Jasmine測試中添加了一個測試文件(/test/UtilsService.spec.js)。它因爲服務未定義而失敗(添加了angular-mocks.js)。這是我的代碼:角度測試茉莉花失敗
karma.conf.js:在/test/UtilsService.spec.js
// 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: [
'node_modules/**/*.js',
'app/**/*.js',
'test/UtilsService.spec.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: {
},
// 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_INFO,
// 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: ['Chrome'],
// 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
測試文件:
describe('getGreeting',function(){
var UtilsService;
beforeEach(angular.mock.module('app.ontdekJouwTalent'));
beforeEach(inject(
function(_UtilsService_) {
UtilsService = _UtilsService_;
}
));
it('should test a dummy function',
function(){
expect(1+1).toEqual(2);
expect(UtilsService.getGreeting("Marc")).toEqual("Hello Marc");
}
)
});
請注意,我註釋掉加載/測試服務,但只加載應用程序:app.ontdekJouwTalent。 在/app/shared/services/UtilsService.js
angular.module('app.ontdekJouwTalent').
service('UtilsService',['AppConfig',function(AppConfig){
this.debug = function(data){
if(AppConfig.APPCONSTANTS_ISLOCAL){
return data;
}
}
this.getGreeting = function(name){
return "Hello " + name;
}
}])
的UtilsService角應用在其他地方定義 - 在/app/app.js這樣的:
angular.module('app.ontdekJouwTalent', [
'angular-storage',
'ui.bootstrap',
'ui.router',
'ui.router.modal',
'xeditable',
'angular-confirm',
'ui.select',
'ngSanitize',
'angular-growl',
'ngAnimate'
])
當從運行此在一個cmd窗口的webroot目錄(wwwroot文件)「報應啓動」我得到
I:\www\ontdekJouwTalent\wwwroot>karma start
04 08 2016 19:23:32.633:WARN [karma]: No captured browser, open http://localhost:9876/
04 08 2016 19:23:32.756:INFO [karma]: Karma v1.1.2 server started at http://localhost:9876/
04 08 2016 19:23:32.757:INFO [launcher]: Launching browser Chrome with unlimited concurrency
04 08 2016 19:23:32.769:INFO [launcher]: Starting browser Chrome
04 08 2016 19:23:38.634:INFO [Chrome 51.0.2704 (Windows 10 0.0.0)]: Connected on socket /#CXn5vEn8tQBLJ23oAAAA with id 50497607
Chrome 51.0.2704 (Windows 10 0.0.0) ERROR
Uncaught ReferenceError: module is not defined
at node_modules/abbrev/abbrev.js:2
所以,「模塊」是不確定的......我不知道該如何處理這個。這裏有什麼問題?
讓我們一起調試。請將'console.log(UtilsService)'放在'UtilsService = _UtilsService_;'行後面,然後告訴我們你看到的是什麼 –
自從我用角度測試的時候已經很短了,但是你肯定需要從服務中返回一些東西? – Pureferret
您的添加內容與之前寫入的內容不同。 'inject'是否立即調用函數?它不應該如此。 –