未定义不是评估Expect.toBeDefined的函数

时间:2019-06-09 14:06:27

标签: javascript angularjs jasmine karma-jasmine karma-runner

我得到了一个使用angularJS的项目,我试图在该项目中运行单元测试,但因果响应会导致错误评估任何匹配器的expec。在这种情况下,我使用的是toBeDefined,但是无论如何我都会遇到相同的错误。我使用的版本是:Karma-Jasmine:^ 2.01,Karma:3.1.4。

describe('EventsCtrl', function() {
beforeEach(module('app'));

var $controller, $rootScope;

beforeEach(inject(function(_$controller_, _$rootScope_){
  $controller = _$controller_;
  $rootScope = _$rootScope_;
}));

describe('$scope.fileName', function() {
  it('bla bla bla', function() {
    var $scope = $rootScope.$new();
    var controller = $controller('EventsCtrl', { $scope: $scope });
    $scope.fileName = 'data';
    expect($scope.fileName).toBeDefined();
  });
});
});

这是我的Karma.conf

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: ['mocha', 'chai', 'jasmine', 'jasmine-matchers'],


// list of files / patterns to load in the browser
files: [
  'bower_components/jquery/dist/jquery.min.js',
  'bower_components/angular/angular.js',
  'bower_components/angular-route/angular-route.min.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'bower_components/angular-messages/angular-messages.min.js',
  'bower_components/angular-sanitize/angular-sanitize.min.js',
  'bower_components/angular-animate/angular-animate.min.js',
  'bower_components/bootstrap/dist/js/bootstrap.min.js',
  'bower_components/angular-bootstrap/ui-bootstrap.min.js',
  'bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
  'bower_components/jstree-directive/jsTree.directive.js',
  'bower_components/ngDialog/js/ngDialog.min.js',
  'bower_components/dygraphs/dygraph-combined.js',
  'bower_components/async/lib/async.js',
  'bower_components/moment/moment.js',
  'bower_components/angular-bootstrap-datetimepicker/src/js/datetimepicker.js',
  'bower_components/ng-csv/build/ng-csv.min.js',
  'bower_components/angular-loading-bar/build/loading-bar.min.js',
  'bower_components/angular-spinkit/build/angular-spinkit.min.js',
  'bower_components/oboe/dist/oboe-browser.min.js',
  'bower_components/angular-oboe/dist/angular-oboe.min.js',
  'bower_components/angular-svg-round-progressbar/build/roundProgress.min.js',
  'bower_components/angular-timer/dist/angular-timer.min.js',
  'bower_components/humanize-duration/humanize-duration.js',
  'bower_components/angular-mailto/angular-mailto.js',
  'bower_components/ng-file-upload/ng-file-upload.min.js',
  'bower_components/qrcode/lib/qrcode.min.js',
  'bower_components/angular-qr/angular-qr.min.js',
  'bower_components/dygraphs/excanvas.js',
  'bower_components/checklist-model/checklist-model.js',
  'bower_components/angularUtils-pagination/dirPagination.js',

  'app/client/scripts/app.coffee',

  'app/client/assets/js/jquery.event.ue.js',
  'app/client/assets/js/jquery.udraggable.js',
  'app/client/assets/js/jquery.metisMenu.js',
  'app/client/assets/js/xeditable/js/xeditable.min.js',
  'app/client/assets/js/jstree/jstree.min.js',
  'app/client/assets/js/ng-table/ng-table.min.js',
  'app/client/assets/js/FileSaver.min.js',
  'app/client/assets/js/switch/bootstrap-switch.min.js',
  'app/client/assets/js/dataView/dataview.js',
  'app/client/assets/js/dataView/dataview-services.js',

  'app/client/scripts/notyFlash.coffee',
  'app/client/scripts/filters.coffee',
  'app/client/scripts/directives.coffee',
  'app/client/scripts/services.coffee',
  'app/client/scripts/controllers.coffee',
  'app/client/scripts/factory.coffee',

  'app/client/scripts/auth/*.coffee',
  'app/client/scripts/config/*.coffee',
  'app/client/scripts/admin/*.coffee',
  'app/client/scripts/data/*.coffee',
  'app/client/scripts/tools/*.coffee',

  'test/*.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: {
  'app/client/scripts/app.coffee': ['coffee'],

  'app/client/scripts/notyFlash.coffee': ['coffee'],
  'app/client/scripts/filters.coffee': ['coffee'],
  'app/client/scripts/directives.coffee': ['coffee'],
  'app/client/scripts/services.coffee': ['coffee'],
  'app/client/scripts/controllers.coffee': ['coffee'],
  'app/client/scripts/factory.coffee': ['coffee'],

  'app/client/scripts/auth/*.coffee': ['coffee'],
  'app/client/scripts/config/*.coffee': ['coffee'],
  'app/client/scripts/admin/*.coffee': ['coffee'],
  'app/client/scripts/data/*.coffee': ['coffee'],
  'app/client/scripts/tools/*.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: false,


// 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: true,

plugins: [
  'karma-chrome-launcher',
  'karma-phantomjs-launcher',
  'karma-jasmine',
  'karma-jasmine-matchers',
  'karma-mocha',
  'karma-chai',
  'karma-coffee-preprocessor'
]
});
};

但是当我运行测试时出现此错误:

enter image description here

0 个答案:

没有答案