简体   繁体   English

具有位置服务的angularjs注射器

[英]angularjs injector with location service

I am testing some Angular.js code with Jasmine. 我正在用Jasmine测试一些Angular.js代码。 To do this I need an Angular injector: 为此,我需要一个角度喷射器:

var injector = angular.injector(['ng', 'ngRoute', 'mainModule']);

This works fine, but when I get to testing a controller that uses $location, I have to add that to the injector: 这可以正常工作,但是当我要测试使用$ location的控制器时,必须将其添加到注入器中:

var injector = angular.injector(['ng', 'ngRoute', 'location', 'mainModule']);

Now this throws an error: 现在这会引发错误:

Error: Error: [$injector:modulerr] Failed to instantiate module location due to:
Error: [$injector:nomod] Module 'location' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

How can I include $location service there? 如何在其中包含$ location服务? Thanks. 谢谢。

-------------- EDIT ----------------- --------------编辑-----------------

due to not many answers I thought I will add some details. 由于答案不多,我想我会添加一些细节。

Here is a controller I am trying to test (or the definition of it, I won't be pasting all code): 这是我要测试的控制器(或它的定义,我不会粘贴所有代码):

var app = angular.module('mainModule');

app.controller('appController', ['$window', '$scope', '$location', '$wsService', '$userService', '$labelService', function ($window, $scope, $location, $wsService, $userService, $labelService) {
    //body of the controller
}]);

You have already seen how I create the injector in my unit test in the original post, here is how I use the injector to create the controller: 您已经在原始文章的单元测试中看到了如何创建注入器,这是我使用注入器创建控制器的方式:

var scope;
var controller;
var getController = function() {
    injector.invoke( ['$rootScope', '$controller', function ($rootScope, $controller) {
        scope      = $rootScope.$new();
        controller = $controller('appController', {$scope: scope});
    }]);
};

Hope this will help with producing some potential answers. 希望这将有助于产生一些潜在的答案。 Thanks 谢谢

To answer my own question. 回答我自己的问题。 Here is how to test the following controller: 这是测试以下控制器的方法:

var app = angular.module('mainModule');

app.controller('appController', ['$window', '$scope', '$location', '$userService', function ($window, $scope, $location, $userService) {
    //body of the controller
}]);

1 In unit test create injector: 1在单元测试中创建注射器:

var injector = angular.injector(['ng', 'ngRoute', 'mainModule']);

2 Invoke services required: 2需要调用服务:

injector.invoke( ['$userService',  function ($userService) {
    service     = $userService;
}]);

3 Mock location service: 3模拟定位服务:

var mockLocation = {
    path : ""
};

I just needed path for the controller I am testing, thus I did not mock anything else, but mock whatever else you need. 我只需要测试控制器的path ,因此我没有模拟其他任何东西,而是模拟了您需要的其他任何东西。

4 Invoke the controller: 4调用控制器:

var scope;
var controller;
var getController = function() {
    injector.invoke( ['$rootScope', '$controller', function ($rootScope, $controller) {
        scope      = $rootScope.$new();
        controller = $controller('appController', {$scope: scope, $location: mockLocation});
    }]);
};

Now the controller can be used in unit tests. 现在,该控制器可用于单元测试。
This did get rid of 'unknown provider' and other errors related to location service. 这确实摆脱了“未知提供者”和与定位服务有关的其他错误。
This blog post helped me with an answer, thanks to the author. 感谢作者, 这篇博客文章为我提供了答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM