gpt4 book ai didi

javascript - 使用 Jasmine/Node : Uncaught type error "Cannot set property of ' mock' undefined 测试 Angular

转载 作者:IT老高 更新时间:2023-10-28 23:19:28 29 4
gpt4 key购买 nike

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

我的配置:

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

我的 index.html header :

<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>

我的测试:

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

在命令提示符下输出:

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)

当使用每个文件的 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)

最佳答案

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

因为我可以看到您正在测试服务,所以在文件下包含该服务的 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
],

关于javascript - 使用 Jasmine/Node : Uncaught type error "Cannot set property of ' mock' undefined 测试 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22181874/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com