javascript – 使用茉莉花/节点测试角度:未捕获类型错误“无法设置”mock“的属性未定义

前端之家收集整理的这篇文章主要介绍了javascript – 使用茉莉花/节点测试角度:未捕获类型错误“无法设置”mock“的属性未定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建“行动中的Angular.js”中描述的茉莉花单元测试.应用程序运行正常,但是当我尝试运行我的测试时,我在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标题

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

解决方法

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

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

files: [

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

猜你在找的JavaScript相关文章