gpt4 book ai didi

javascript - AngularJS 测试 - 注入(inject) $location 导致错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:43 25 4
gpt4 key购买 nike

所以这个让我发疯,每当我尝试将 $location 注入(inject)我的测试之一时,我都会收到以下错误:

TypeError: undefined is not a function
at $LocationProvider.$get (bower_components/angular/angular.js:11053:34)
at Object.invoke (bower_components/angular/angular.js:4118:17)
at bower_components/angular/angular.js:3936:37
at getService (bower_components/angular/angular.js:4077:39)
at Object.invoke (bower_components/angular/angular.js:4109:13)
at Object.workFn (bower_components/angular-mocks/angular-mocks.js:2159:20)
Error: Declaration Location
at window.inject.angular.mock.inject (bower_components/angular-mocks/angular-mocks.js:2144:25)
at Suite.<anonymous> (test/unit/controllers/DashboardCtrlSpec.js:8:16)
at jasmineInterface.describe (/usr/local/lib/node_modules/karma-jasmine/lib/boot.js:59:18)
at test/unit/controllers/DashboardCtrlSpec.js:1:1

karma .conf.js:

module.exports = function (config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-bootstrap/ui-bootstrap.js',
'bower_components/angular-sanitize/angular-sanitize.js',
'bower_components/angular-translate/angular-translate.js',
'bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.js',
'bower_components/dist/assets.angular.max.js',
'scripts/**/*.js',
'test/**/*.js'
],


// list of files to exclude
exclude: [
'**/*.swp'
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};

Controller :

angular.module('ecQDesktopApp')
.controller('DashboardCtrl', function ($scope, $window) {

$scope.goToURL = function (url) {
$window.location = url;
}

}
);

功能规范文件:

describe('Unit: Testing Dashboard Controller', function() {
'use strict';

var scope, window;

beforeEach(module('ecQDesktopApp'));

beforeEach(inject(function($rootScope, _$window_, $controller) {
scope = $rootScope.$new();
window = {
location: ""
};
$controller('DashboardCtrl', {$scope: scope, $window: window});
}));

it('goes to Url', function() {
var url = "testUrl";
scope.goToURL(url);
expect(window.location).toBe("testUrl");
});
});

但是如果我将 beforeEach(inject(function($rootScope, _$window_, $controller) 更改为 beforeEach(inject(function($rootScope, _$window_, $controller, $location) 我收到错误。

这很奇怪,看起来我可以注入(inject)任何 AngularJS 服务,但每当我尝试在我的任何测试中使用 $location 时,我都会崩溃。

提前致谢。

最佳答案

如果你在 >= 1.3 的版本中使用 angular,你还必须在 >= 1.3 的版本中使用 angular-mocks

一般来说:angular-mocks 的版本应始终与 angular 处于同一级别。

关于javascript - AngularJS 测试 - 注入(inject) $location 导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26738493/

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