AngularJS测试 - 注射$位置会导致错误 [英] AngularJS test - inject $location causes error

查看:182
本文介绍了AngularJS测试 - 注射$位置会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这个人是推动我疯了,每当我试图注入 $位置来我的测试之一,我得到了以下错误:

So this one is driving me crazy, whenever i try to inject $location to one of my tests i get the following error:

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:

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
    });
};

控制器:

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(注(函数($ rootScope,_ $ window_,$控制器) beforeEach(注(函数($ rootScope,_ $ window_,$控制器,$位置)我得到的错误。

这很奇怪,它看起来像我可以注入任何AngularJS服务,但每当我试图在我的任何测试使用$位置我得到崩溃。

It's weird, it looks like i can inject any AngularJS services but whenever i try to use $location in any of my tests i get that crash.

先谢谢了。

推荐答案

如果您在版本> = 1.3采用了棱角分明,你还一个版本使用角嘲笑> = 1.3

If you are using angular in a version >= 1.3, you have also to use angular-mocks in a version >= 1.3

在一般:
角嘲笑的版本应始终是在相同的水平的角

In general: The version of angular-mocks should always be at the same level as angular.

这篇关于AngularJS测试 - 注射$位置会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆