Javascript Karma + angular-mocks TypeError:'undefined' 不是一个对象(评估 'angular.mock = {}')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27823741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:43:20  来源:igfitidea点击:

Karma + angular-mocks TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}')

javascriptangularjsunit-testingjasminekarma-runner

提问by Daniel Bogart

I'm trying to write unit tests using Karma + Jasmine, but running into an error with angular-mocks. When running grunt test I get the following error:

我正在尝试使用 Karma + Jasmine 编写单元测试,但是在使用 angular-mocks 时遇到了错误。运行 grunt 测试时出现以下错误:

PhantomJS 1.9.8 (Mac OS X) ERROR TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}') at /Users/danielbogart/Documents/coding/work/AexNav/bower_components/angular-mocks/angular->mocks.js:17 Chrome 39.0.2171 (Mac OS X 10.9.4) ERROR Uncaught TypeError: Cannot set property 'mock' of undefined at /Users/danielbogart/Documents/coding/work/AexNav/bower_components/angular-mocks/angular->mocks.js:17

PhantomJS 1.9.8 (Mac OS X) 错误类型错误:在 /Users/danielbogart/Documents/coding/work/AexNav/bower_components/angular-mocks/ 处,'undefined' 不是一个对象(评估 'angular.mock = {}') angular->mocks.js:17 Chrome 39.0.2171 (Mac OS X 10.9.4) 错误未捕获类型错误:无法在 /Users/danielbogart/Documents/coding/work/AexNav/bower_components/angular- 设置未定义的属性“模拟”模拟/角度->mocks.js:17

Gruntfile karma config:

Gruntfile 业力配置:

    karma: {
        options: {
            frameworks: ['jasmine'],
            files: [ 
                'dom_munger.data.appjs',
                'tests/spec/*.js',
                'bower_components/angular-mocks/angular-mocks.js'
            ],
            logLevel: 'ERROR',
            reporters: ['mocha'],
            autoWatch: false, //watching is handled by grunt-contrib-watch
            singleRun: true
        },
        all_tests: {
            browsers: ['PhantomJS', 'Chrome']
        },
        during_watch: {
            browsers: ['PhantomJS']
        }
    }

Thanks!

谢谢!

回答by PSL

You would need to include angular.js as well, then only angular.mocks will work else window.angular will be undefined.

您还需要包含 angular.js,然后只有 angular.mocks 才能工作,否则 window.angular 将是未定义的。

 files: [ 
            'dom_munger.data.appjs',
            'path/to/angular.js', //<-- include angularjs
            'bower_components/angular-mocks/angular-mocks.js',
            'tests/spec/*.js'
        ],

回答by Amit Portnoy

update for 2017 and webpack 2(but same angular<2)

2017 和 webpack 2 的更新(但相同的 angular<2)

  1. like @jlew said, angular-mocks expects angular on the window

  2. module reference is being replaces by webpack

  1. 就像@jlew 所说的,angular-mocks 期望窗口上有 angular

  2. 模块引用正在被 webpack 取代

So in tests you need an header like:

所以在测试中你需要一个像这样的标题:

import angular from 'angular';
import 'angular-mocks/ngMock';

and replace each global modulereference with angular.mock.module

并将每个全局module引用替换为angular.mock.module

回答by jlew

angular-mocks.js presumes that angular.js has been included as well.

angular-mocks.js 假定 angular.js 也已包含在内。

回答by Thibault

I had this error on the following statement :

我在以下语句中遇到此错误:

angular.mock.module( 'ui.router' )

angular.mock.module( 'ui.router' )

This was caused by a wrong order in libraries declaration :

这是由库声明中的错误顺序引起的:

karma.config.js :

业力.config.js :

...
files: ['./node_modules/angular-mocks/angular-mocks.js']
   .concat(dependencies),
...

(dependencieswas the list of my libs and containing angular.js)

dependencies是我的库列表并包含angular.js

I simply move './node_modules/angular-mocks/angular-mocks.js'into the dependencies list, but AFTERangular.js:

我只是'./node_modules/angular-mocks/angular-mocks.js'进入依赖项列表,但之后angular.js

Like this :

像这样 :

...
'./node_modules/angular/angular.js',
'./node_modules/angular-mocks/angular-mocks.js'
...


To go further :

更进一步:

If you look at angular-mocks.jsyou can see it needs angular.jsto work !

如果你看看angular-mocks.js你会发现它需要angular.js工作!

/**
 * @license AngularJS v1.7.5
 * (c) 2010-2018 Google, Inc. http://angularjs.org
 * License: MIT
 */
(function(window, angular) {

'use strict';

/* global routeToRegExp: true */
...