【问题标题】:"before each" hook: workFn error“在每个之前”挂钩:workFn 错误
【发布时间】:2016-06-07 07:25:03
【问题描述】:

我正在使用 karma 和 phantom 运行我的测试,另外我正在使用 mocha 和 sinon 并且测试失败并出现以下错误:

EditResourceCategoryDialogTest EditResourceCategoryDialogController "before each" hook: workFn
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.9/$injector/modulerr?p0=resourceofferingsApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20

示例代码:

define(function (require) {
    "use strict";

    var assert = require('chai').assert;
    var sinon = require('sinon');
    var angular = require('angular');
    var angularMocks = require('angular.mocks');

    require('resourceofferings/app');
    require('dialog path');

    describe('EditResourceCategoryDialogTest', function () {

        beforeEach(module('resourceofferingsApp'));

        describe('EditResourceCategoryDialogController', function () {
            var $scope, ctrl;

            //you need to inject dependencies first
            beforeEach(inject(function ($rootScope, $injector) {
                $scope = $rootScope.$new();
            }));

            it('initialization test (create mode)', inject(function ($controller) {

                ctrl = $controller("EditResourceCategoryDialogController", {
                    $scope: $scope,
                    $uibModalInstance: null,
                    options: {
                        isEditMode: false
                    }
                });

                assert.equal($scope.isEditMode, false);
            }));

        });
    });
});

这里完全失败了:

beforeEach(inject(function ($rootScope, $injector) {
    $scope = $rootScope.$new();
}));

请帮我解决这个问题..

提前致谢。

【问题讨论】:

    标签: angularjs mocha.js karma-runner


    【解决方案1】:

    试试这个...

    describe('controllers', function(){
        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new(); // this is what you missed out
            controller = $controller('EditResourceCategoryDialogController', {
                $scope: scope,
                $uibModalInstance: null,
                options: {
                    isEditMode: false
                }
            });
        }));
    });
    

    更新:根据 Angular ...

    模块无法加载的一个常见原因是您忘记了 将文件包含在已定义的模块中或文件不能 被加载。

    您确定所有需要的文件都已加载吗?

    【讨论】:

    • 谢谢史密斯。但我仍然遇到同样的错误:“每个之前”钩子:workFn
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多