javascript - Jasmine 无法实例化 Angular 模块?

标签 javascript angularjs unit-testing jasmine

好吧,我正在尝试使用 Jasmine 2.2.0 为 AngularJS 项目实现单元测试 - 但我无法获得适用于 Controller 的基本示例测试。如有任何帮助,我们将不胜感激。

这是 Controller 的代码(我拥有的最简单的代码):

angular.module('app.alerts')
    .controller('AlertCtrl', AlertCtrl);

AlertCtrl.$inject = ['$scope', 'AlertService'];

function AlertCtrl($scope, AlertService) {

    var vm = this;
    vm.closeAlert = function(index) {
        AlertService.closeAlert(index);
    }

    vm.getAlertList = function() {
        return AlertService.getAlertList();
    }
}

这是我尝试运行的规范文件:

describe('myApp', function() {
    describe('controller', function() {

        var scope, controller;
        var mockAlertService = {      // simple mock service
            closeAlert: function(e) {
                console.log('close');
            },
            getAlertList: function() {
                return [];
            }
        }    

        beforeEach(function() {
            angular.mock.module('app.alert');
        });

        beforeEach(inject(function($rootScope, $controller) {
            scope = $rootScope.$new();
            controller = $controller('AlertCtrl as alertctrl', {
                $scope: scope,
                AlertService: mockAlertService
            });
        }));

        it('should work: ', function() {
            expect(true).toBe(true);
        });
    });
});

当我运行测试时,我收到以下错误消息:

Error: [$inject:modulerr] Failed to instantiate module app.alert due to:
[$injector:nomod] Module 'app.alert' is not available! You either misspelled
the module name or forgot to load it.

注入(inject)器似乎不知道 app.alert 模块,这意味着它没有被正确模拟?我将 angular-mocks.js 文件包含在我的 SpecRunner.html 文件中。有人能看出我做错了什么吗?

最佳答案

你是不是拼错了 app.alertapp.alerts:

angular.module('app.alerts')
    .controller('AlertCtrl', AlertCtrl);

angular.mock.module('app.alert');

关于javascript - Jasmine 无法实例化 Angular 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455847/

相关文章:

javascript - 当 window.onbeforeunload 返回 false 时,Safari 保持 "loading"

javascript - 应用程序在 Android 版本 4.4.2 上运行时中断,抛出错误 : [ng:areq]

c# - 使用 Moq 和 MSTest 测试异常的正确方法

php - 在 PHPUnit 中断言数组 equalTo 且某些值为 null

javascript - 数组不是 Javascript 中的数据类型?

javascript - 非常简单的javascript来删除文本框中的值

javascript - 配置注释错误 : Argument 'fn' is not a function, 得到字符串

javascript - 当它在 AngularJS 中返回时,如何获得异步服务调用来更新 View ?

java - 具有删除目录方法的单元测试文件实用程序类

javascript - 数组放置会改变输出吗?