javascript - 将名称参数添加到范围时发生 AngularJS 测试错误?

标签 javascript angularjs karma-runner angularjs-controller

运行 karma start configs/karma.conf.js 时收到错误:

INFO [PhantomJS 1.9.2 (Linux)]: Connected on socket PK-IbFeI1qpFq0fix6WI
PhantomJS 1.9.2 (Linux) IndexController should add name parameter to scope FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/1.2.6/$injector/modulerr?p0=myapp.controllers&p1=Error%3A%20%5B%24injector%3Anomod%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.2.6%2F%24injector%2Fnomod%3Fp0%3Dmyapp.controllers%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9877%2Fbase%2Fpublic%2Fapp%2Flib%2Fangular%2Fangular.min.js%3F1387689655000%3A20%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9877%2Fbase%2Fpublic%2Fapp%2Flib%2Fangular%2Fangular.min.js%3F1387689655000%3A21%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A9877%2Fbase%2Fpublic%2Fapp%2Flib%2Fangular%2Fangular.min.js%3F1387689655000%3A29
    at /home/Projects/abbrevi8-karma-tests/public/app/lib/angular/angular.min.js:29
TypeError: 'undefined' is not an object (evaluating 'scope.name')
    at /home/Projects/abbrevi8-karma-tests/test/unit/controllerSpec.js:19
PhantomJS 1.9.2 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.178 secs / 0.006 secs)

public/app/controllers.js

var myApp = angular.module('myApp', []);

myApp.controller('IndexController', function ($scope) {
        $scope.name = 'bob';
});

测试/单元/controllerSpec.js

'use strict';

describe('IndexController', function() {
    //initialise module
    beforeEach(module('myapp.controllers'));

    //params initialised in scope for tests
    var ctrl, scope;

    beforeEach(inject(function($controller) {
        //get controller from $controller provider
        scope = {}; 
        ctrl = $controller('IndexController', {
            $scope: scope
        }); 
    }));

    it ('should add name parameter to scope', function() {
        expect(scope.name).toBeDefined(); 
    }); 
});

完整项目(与上面相同,仅使用我的 karma.conf.jssetup.sh):https://gist.github.com/AlecTaylor/8078807

最佳答案

尝试:

beforeEach(module('myApp'));

而不是:

beforeEach(module('myapp.controllers'));

关于javascript - 将名称参数添加到范围时发生 AngularJS 测试错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20726639/

相关文章:

javascript - 无法获取 Html.Kendo().ComboBoxFor 的选定值

javascript - 循环以从对象数组中收集所有图像属性

angularjs - Ionic 应用程序在 Android 4 版本上显示白屏

angular - NO_ERRORS_SCHEMA 停止 Karma 测试以 Angular 工作

javascript - 如何将数据从 JS 发送到 Bash?

javascript - 无法使用 Angular.js 在数组中有序获取文件

Angularjs - 隐藏内容直到 DOM 加载

python:带有非默认参数的defaultdict

node.js - npm 检查并根据需要更新包

javascript - 调用 AppRegistry.registerComponent 失败