Angular,Karma单元测试 - 不是一个功能

时间:2017-01-23 04:27:48

标签: 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 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)

单元测试apis.spec.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');
 });

});

我的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
})
}

test-context.js

var context = require.context('./app/src', true, /\.spec\.js$/);
context.keys().forEach(context);

EDIT1

最后我找到了我做得不好的事情,只是将apis.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');
  });
});

0 个答案:

没有答案