Angularjs 角度,因果报应单元测试-不是一个函数
我对AngularJS、ES6和Karma都是新手。我和因果报应斗争了3天,所以这真的很烦人。我想在我的angular videos library应用程序中添加一些单元测试 控制台出现以下错误:Angularjs 角度,因果报应单元测试-不是一个函数,angularjs,ecmascript-6,karma-runner,Angularjs,Ecmascript 6,Karma Runner,我对AngularJS、ES6和Karma都是新手。我和因果报应斗争了3天,所以这真的很烦人。我想在我的angular videos library应用程序中添加一些单元测试 控制台出现以下错误: Chrome 55.0.2883 (Windows 10 0.0.0) Youtube api service should get an ID from long youtube link FAILED TypeError: _youtube.YoutubeService.getId is not
Chrome 55.0.2883 (Windows 10 0.0.0) Youtube api service should get an ID from long youtube link FAILED
TypeError: _youtube.YoutubeService.getId is not a function at Object.<anonymous> (C:/projects/angular-videos-library/test-context.js:94:39)
Chrome 55.0.2883 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.009 secs / 0.002 secs)
23 01 2017 05:16:35.400:INFO [Firefox 50.0.0 (Windows 10 0.0.0)]: Connected on socket cmmhcJeWbGfWGgJWAAAB with id 34881
Firefox 50.0.0 (Windows 10 0.0.0) Youtube api service should get an ID from long youtube link FAILED
TypeError: _youtube.YoutubeService.getId is not a function in C:/projects/angular-videos-library/test-context.js
(line 94)
@C:/projects/angular-videos-library/test-context.js:94:15
Chrome 55.0.2883 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.009 secs / 0.002 secs)
Firefox 50.0.0 (Windows 10 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.003 secs / 0.001 secs)
}))
我的karma.conf.js:
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: [
'app/js/vendor.js',
'node_modules/angular-mocks/angular-mocks.js',
{ pattern: 'test-context.js', watched: false }
],
// 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-context.js': ['webpack']
},
webpack: {
module: {
loaders: [
{ test: /\.js/, exclude: /node_modules/, loader: 'babel-loader' }
]
},
watch: true
},
webpackServer: {
noInfo: true
},
// 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: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'Firefox'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
和testcontext.js
:
import { YoutubeService } from './youtube.service';
describe('Youtube api service', () => {
beforeEach(angular.mock.module('app'));
it('should get an ID from long youtube link', () => {
let link = 'https://www.youtube.com/watch?v=TFeSNOdNtyo';
let id = YoutubeService.getId(link);
expect(id).toBe('TFeSNOdNtyo');
});
var context = require.context('./app/src', true, /\.spec\.js$/);
context.keys().forEach(context);
编辑1
最后,我发现我做得不好,只是将api.spec.js
更改为:
import { YoutubeService } from './youtube.service.js';
let service;
describe('Youtube api service', () => {
beforeEach(angular.mock.module('app'));
beforeEach(() => {
service = new YoutubeService();
});
it('should get an ID from long youtube link', () => {
let link = 'https://www.youtube.com/watch?v=TFeSNOdNtyo';
let id = service.getId(link);
expect(id).toBe('TFeSNOdNtyo');
});
});
你能给我们看看你的YouTube服务吗?在单元测试中,你不应该直接调用live服务。相反,你应该模拟它。