gpt4 book ai didi

angularjs - Angular 单元测试中的未知提供者

转载 作者:行者123 更新时间:2023-12-03 09:32:45 24 4
gpt4 key购买 nike

我有这个 Controller :

angular.module("controllers").controller("AddController", function($rootScope, $scope, $location, $timeout, $routeParams, Todos, MessageQueue) {
// ... code
});

和这个测试:
describe("AddController", function() {

var ctrl;
var scope;

beforeEach(module('controllers'));

beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('AddController', {
$scope: scope
});
}));

it("should be available", function() {
expect(ctrl).not.toBe(undefined);
});

});

业力 说:
Error: [$injector:unpr] Unknown provider: $routeParamsProvider <- $routeParams
http://errors.angularjs.org/1.2.27-build.533+sha.c8c2386/$injector/unpr?p0=%24routeParamsProvider%20%3C-%20%24routeParams

但在我的 karma.config 我导入 bower_components/angular-route/angular-route.js .这里:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'bower_components/angular/angular.js',
'bower_components/angular-route/angular-route.js',
'bower_components/angular-mocks/angular-mocks.js',

'app/scripts/app.js',
'app/scripts/services/mod.js',
'app/scripts/directives/mod.js',
'app/scripts/controllers/mod.js',

'app/scripts/**/*.js',
'test/spec/**/*Spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Firefox'],
singleRun: false
});
};

我有点不解。任何人?

最佳答案

您可以自己将 '$routeParams' 对象传递给 $controller调用,它看起来像这样:

beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('AddController', {
$scope: scope,
$routeParams: {id: '...'}
});
}));

关于angularjs - Angular 单元测试中的未知提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26627468/

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