gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:32:22 25 4
gpt4 key购买 nike

好吧,我正在尝试使用 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/

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