TypeError:angular.element.cleanData不是函数

时间:2016-04-04 21:10:06

标签: angularjs karma-runner karma-mocha angular-mock

我尝试使用inject()

时,我的业力单元测试中出现以下错误
  Example
    ✗ should wait for promise to resolve and have a result
    Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

  ✗ "after each" hook for "should wait for promise to resolve and have a result"
    TypeError: angular.element.cleanData is not a function
        at Function.module.$$cleanup (base/app/bower_components/angular-mocks/angular-mocks.js?aa216eb2339283c5ab5aa192023171aa202a2bbd:2776:23)
        at Context.module.$$afterEach (base/app/bower_components/angular-mocks/angular-mocks.js?aa216eb2339283c5ab5aa192023171aa202a2bbd:2746:14)

以下是代码:

describe('Example', function() {
  var $q;
  var $rootScope;
  var fakePromise;

  beforeEach(inject(function (_$q_, _$rootScope_) {
    $q = _$q_;
    $rootScope = _$rootScope_;

    fakePromise = function fakePromise(){
      var def = $q.defer();
      setTimeout(function(){
        def.resolve('foo');
      }, 100);

      return def.promise;
    };
  }));

  it('should wait for promise to resolve and have a result', function(){
    return fakePromise().should.eventually.equal('foo');
  });
});

我使用角度和角度模拟1.5.3。

这是我的业力配置:

  // Karma configuration
  // Generated on Tue Mar 29 2016 14:28:04 GMT-0700 (PDT)

  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', 'chai-as-promised', 'sinon'],


      // list of files / patterns to load in the browser
      files: [
        'app/bower_components/angular/angular.js',
        'app/bower_components/angular-mocks/angular-mocks.js',
        'app/bower_components/es5-shim/es5-shim.js',
        'app/bower_components/jquery/dist/jquery.js',
        'app/bower_components/x2js/xml2json.js',
        'app/bower_components/angular/angular.js',
        'app/bower_components/angular-messages/angular-messages.js',
        'app/bower_components/angular-bootstrap/ui-bootstrap-tpls.js',
        'app/bower_components/angular-cookies/angular-cookies.js',
        'app/httpsnippet-browser/dist/restlet-angular-httpsnippet.js',
        'app/bower_components/highlightjs/highlight.pack.js',
        'app/bower_components/angular-highlightjs/angular-highlightjs.js',
        'app/bower_components/marked/lib/marked.js',
        'app/bower_components/angular-marked/angular-marked.js',
        'app/bower_components/angular-pretty-xml/src/angular-pretty-xml.js',
        'app/bower_components/angular-resource/angular-resource.js',
        'app/bower_components/angular-sanitize/angular-sanitize.js',
        'app/bower_components/ace-builds/src-noconflict/ace.js',
        'app/bower_components/ace-builds/src-noconflict/mode-yaml.js',
        'app/bower_components/ace-builds/src-noconflict/ext-language_tools.js',
        'app/bower_components/ace-builds/src-noconflict/ext-searchbox.js',
        'app/bower_components/angular-ui-ace/ui-ace.js',
        'app/bower_components/angular-ui-router/release/angular-ui-router.js',
        'app/bower_components/angular-ui-validate/dist/validate.js',
        'app/bower_components/angular-ui-uploader/dist/uploader.js',
        'app/bower_components/angular-inview/angular-inview.js',
        'app/bower_components/ng-resize/index.js',
        'app/bower_components/bootstrap/dist/js/bootstrap.js',
        'app/bower_components/js-yaml/dist/js-yaml.js',
        'app/bower_components/json3/lib/json3.js',
        'app/bower_components/ngstorage/ngStorage.js',
        'app/bower_components/yaml-js/yaml.js',
        'app/bower_components/async/lib/async.js',
        'app/bower_components/traverse/traverse.js',
        'app/bower_components/path-loader/browser/path-loader.js',
        'app/bower_components/json-refs/browser/json-refs.js',
        'app/bower_components/lodash/lodash.js',
        'app/bower_components/spark-md5/spark-md5.js',
        'app/bower_components/swagger-converter/browser.js',
        'app/bower_components/z-schema/dist/ZSchema-browser-min.js',
        'app/bower_components/visionmedia-debug/dist/debug.js',
        'app/bower_components/swagger-tools/browser/swagger-tools.js',
        'app/bower_components/apply-diff/index.js',
        'app/bower_components/json-editor/dist/jsoneditor.js',
        'app/bower_components/schema-form/dist/schema-form.js',
        'app/bower_components/ng-file-upload/ng-file-upload.js',
        'app/bower_components/json-formatter/dist/json-formatter.js',
        'app/bower_components/raf/index.js',
        'app/bower_components/angular-ui-layout/ui-layout.js',
        'app/bower_components/json-schema-view/dist/json-schema-view.js',
        'app/bower_components/objectpath/lib/ObjectPath.js',
        'app/bower_components/angular-native-dragdrop/draganddrop.js',
        'app/bower_components/d3/d3.js',
        'app/bower_components/angular-translate/angular-translate.js',
        'app/bower_components/json-schema-defaults/lib/defaults.js',
        'app/bower_components/tv4/tv4.js',
        'app/bower_components/angular-schema-form/dist/schema-form.js',
        'app/bower_components/angular-schema-form/dist/bootstrap-decorator.js',
        'app/bower_components/angular-animate/angular-animate.js',
        'app/bower_components/angular-aria/angular-aria.js',
        'app/bower_components/angular-material/angular-material.js',
        'app/bower_components/moment/min/moment-with-locales.js',
        'app/bower_components/js-base64/base64.js',
        'app/bower_components/sway/browser/sway.js',
        'app/bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js',
        'app/bower_components/validator-js/validator.min.js',
        'app/httpsnippet-browser/dist/restlet-angular-httpsnippet.js',
        'app/ace/theme-atom_dark.js',
        'app/core/**/*.js',
        'app/**/*UnitSpec.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: {
        // '**/*.js': ['coverage']
      },


      // test results reporter to use
      // possible values: 'dots', 'progress'
      // available reporters: https://npmjs.org/browse/keyword/karma-reporter
      // reporters: ['progress', 'coverage'],
      reporters: ['spec'],


      // 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'],

      // // optionally, configure the reporter
      // coverageReporter: {
      //   type : 'html',
      //   dir : 'coverage/'
      // },

      // 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
    })
  }

7 个答案:

答案 0 :(得分:17)

我遇到了同样的问题,修复它的方法是在我的测试中加载jquery:

M

this comment接受了这个想法,因为我们也使用jQuery和Angular。

(将我的Angular版本从1.5.3降级到1.5.0也完成了这项工作,但现在使用我的解决方案我可以使用最新的Angular版本......)

答案 1 :(得分:6)

删除重复的1.4.x并确保所有有角度的库都使用1.3.15修复了我的问题。我有一些使用1.4.7和其他{{1}}

答案 2 :(得分:6)

使用相同的角度和角度模拟版本解决了我的问题

答案 3 :(得分:2)

我遇到了同样的问题,修复了以下内容:

转到angular-mocks.js文件并替换:

angular.element.cleanData(cleanUpNodes);

if (angular.element.cleanData) angular.element.cleanData(cleanUpNodes);

这篇文章有人建议:https://rafikitechnology.com/2016/04/26/fixed-issue-with-angular-and-angular-mocks-do-not-agree-jasmine-is-angry/

答案 4 :(得分:1)

我认为这与https://github.com/angular/angular.js/issues/14251的问题相同(错误消息看起来有所不同,具体取决于您用于Karma测试的浏览器)。 我将我的Angular版本从1.5.3降级到1.5.0,错误消失了。我想我们必须等到新版本修复它。

答案 5 :(得分:1)

我正在使用一个非常古老的遗留应用程序,我的问题分为两部分:

  1. 应用程序母版页中包含的jQuery版本也是如此 旧的,所以我需要在angular之前导入一个更新的版本。
  2. 我还将我的jQuery UI包含在angular.js包含上面(有一个错误,这曾经是一个问题,应该在Angular的更高版本中修复)。

答案 6 :(得分:0)

我遇到了同样的问题,我通过在karma配置文件的角度之前添加jquery denpendency来修复它。

  files: [
  { pattern: 'libs/jquery/jquery/dist/jquery.min.js', watched: false },
  { pattern: 'libs/angular/angular/angular.js', watched: false },
  .....]