javascript 使用 Jasmine/Node 测试 Angular:未捕获的类型错误“无法设置 'mock' 的属性未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22181874/
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-10-27 22:38:07  来源:igfitidea点击:

Testing Angular with Jasmine/Node: Uncaught type error "Cannot set property of 'mock' undefined

javascriptnode.jsangularjsunit-testingjasmine

提问by Jamie M.

I am trying to create the Jasmine unit tests described in "Angular.js in Action". The app runs properly but I keep getting this error in the node.js command prompt when trying to run my test.

我正在尝试创建“Angular.js in Action”中描述的 Jasmine 单元测试。该应用程序运行正常,但在尝试运行我的测试时,我在 node.js 命令提示符中不断收到此错误。

My config:

我的配置:

module.exports = function(config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '',


// frameworks to use
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [

  'javascripts/angular.min.js',
  'javascripts/angular-mocks.js',
  'javascripts/app.js',
  'tests/angelloModelSpec.js',
  ],

My index.html header:

我的 index.html 标题:

<head>
    <script src="javascripts/angular.min.js" type="text/javascript"></script>
    <script src="javascripts/angular-mocks.js"></script>
    <script src="javascripts/app.js"></script>


    <meta charset="utf-8">
    <title>Angello</title>
    <meta name="description" content="Angello Story Application">
    <link rel="stylesheet" href="app.css">
</head>

My test:

我的测试:

describe('Service: angelloModel', function(){

// load the service's module
beforeEach(module('Angello'));

var modelService;

// Initialize the service
beforeEach(inject(function (angelloModel){
    modelService = angelloModel;
}));

describe('#getStatuses', function(){
    it('should return seven different statuses', function () {
        expect(modelService.getStatuses().length).toBe(7);
    });
});
});

output in command prompt:

命令提示符中的输出:

Your environment has been set up for using Node.js 0.10.26 (x64) and npm.

Your environment has been set up for using Node.js 0.10.26 (x64) and npm.

C:\Users\jmclaughlin>karma start karma.conf.js
INFO [karma]: Karma v0.10.9 server started at http://localhost:****/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Windows 7)]: Connected on socket ***************
Chrome 33.0.1750 (Windows 7) ERROR
    Uncaught TypeError: Cannot set property 'mock' of undefined
    at C:/Angello/javascripts/angular-mocks.js:17
Chrome 33.0.1750 (Windows 7): Executed 0 of 0 ERROR (0.465 secs / 0 secs)

When using version 1.0.7 of each file:

使用每个文件的 1.0.7 版时:

Your environment has been set up for using Node.js 0.10.26 (x64) and npm.

C:\Users\myUsername>karma start karma.conf.js
INFO [karma]: Karma v0.10.9 server started at http://localhost:****/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 33.0.1750 (Windows 7)]: Connected on socket ***************
Chrome 33.0.1750 (Windows 7) ERROR
    Uncaught ReferenceError: angular is not defined
    at C:/Angello/javascripts/angular-mocks.js:16
Chrome 33.0.1750 (Windows 7): Executed 0 of 0 ERROR (0.064 secs / 0 secs)

回答by ailveen

Normally this is caused by conflicting versions of angular, a wrong order of scripts definition in karma.conf.js, an incomplete import, or a syntax error.

通常这是由 angular 版本冲突、karma.conf.js 中脚本定义的错误顺序、导入不完整或语法错误引起的。

Since I can see that you are testing a service, include that services' js file under the files (unless it is embedded in app.js) and remove the misplaced comma (see below):

由于我可以看到您正在测试服务,因此在文件下包含该服务的 js 文件(除非它嵌入在 app.js 中)并删除错位的逗号(见下文):

files: [

'javascripts/angular.min.js',
'javascripts/angular-mocks.js',
'javascripts/app.js',
'<include your service js here>',
'tests/angelloModelSpec.js', <<-- and remove this trailing comma
],

回答by Jamie M.

I found the problem! i was editing the wrong config file! I was editing a config file inside the app directory but the real config file was in C:\Users\myUsername\karma.conf.js

我发现了问题!我正在编辑错误的配置文件!我正在编辑 app 目录中的配置文件,但真正的配置文件在 C:\Users\myUsername\karma.conf.js

Thank you for your help!

谢谢您的帮助!