gpt4 book ai didi

unit-testing - 使用 Jasmine 和 Karma 测试模块化 AngularJS

转载 作者:行者123 更新时间:2023-12-03 22:33:48 26 4
gpt4 key购买 nike

努力在 Jasmine/Karma 中设置单元测试。我有一个具有服务依赖关系的 Controller ,并且该服务具有另一个服务依赖关系。我不是按类型(指令、服务等)组织我的模块,而是按功能(布局、摘要 View 等)组织我的模块。

这是架构:

angular.module('myApp', ['ngResource', 'myApp.base', 'myApp.layout','myApp.common']);
angular.module('myApp.base', ['myApp.common']);
angular.module('myApp.common',[]);
angular.module('myApp.layout',['myApp.common']);

Controller :
angular.module('myApp.layout')
.controller('LayoutCtrl', ['$scope', '$rootScope', '$timeout', 'layoutService', 'urlService', 'BaseService',
function ($scope, $rootScope, $timeout, layoutService, urlService, BaseService) {
//controller code here
});

排版服务:
 angular.module('myApp.layout')
.service('layoutService', ['$http', '$resource', '$rootScope', '$location', '$route', 'errorHandlingService', 'utilService',
function ($http, $resource, $rootScope, $location, $route, errorHandlingService, utilService) {
//service code here
});

据我了解,如果我只包括 beforeEach(module('myApp.layout')); ,我应该可以访问我的布局模块中的 Controller 、服务、过滤器和指令。

相反,以下代码失败:
describe('Layout Controller', function() {
var ctrl, scope, service;

beforeEach(module('myApp'));
beforeEach(module('myApp.layout'));
beforeEach(inject(function($controller, $rootScope, layoutService) {

scope = $rootScope.$new();
service = layoutService;
//Create the controller with the new scope
ctrl = $controller('LayoutCtrl', {$scope: scope, layoutService: service});
dump(scope);
}));
it('should exist', function() {
expect(ctrl).toBeDefined();
});
});

出现此错误:
Chrome 26.0 (Mac) Layout Controller should exist FAILED
Error: Unknown provider: layoutServiceProvider <- layoutService
at Error (<anonymous>)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:236
at Object.c [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:317
at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13)
at Object.d [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:147)
at workFn (http://code.angularjs.org/1.0.4/angular-mocks.js:1754:20)
Error: Declaration Location
at window.jasmine.window.inject.angular.mock.inject (http://code.angularjs.org/1.0.4/angular-mocks.js:1740:25)
at null.<anonymous> (/Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:6:14)
at /Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:1:1
Expected undefined to be defined.
Error: Expected undefined to be defined.
at null.<anonymous> (/Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:15:16)
Chrome 26.0 (Mac): Executed 10 of 10 (1 FAILED) (0.36 secs / 0.014 secs)

想法?

最佳答案

Unknown Provider 错误通常来自文件未加载或加载顺序不正确。在测试运行时检查哪些文件存在。

关于unit-testing - 使用 Jasmine 和 Karma 测试模块化 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16625598/

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