UnknownError:javascript错误:[$ injector:unpr]未知提供程序

时间:2014-10-23 12:31:24

标签: javascript angularjs protractor

尝试为我的角度应用程序创建一个量角器测试,这是我试图从我的Jasmine脚本调用的服务:

var app =angular.module('app',[]);

app.factory('myService', function($http) {
        var myService = {
            async: function() {
                // $http returns a promise, which has a then function, which also returns a promise
                var promise = $http.get('test.json').then(function (response) {
                    // The then function here is an opportunity to modify the response
                    console.log(response);
                    // The return value gets picked up by the then in the controller.
                    return response.data;
                });
                // Return the promise to the controller
                return promise;
            }
        };
        return myService;
    });

;

我的规格定义如下:

it('should call api i the site', function () {
    browser.executeAsyncScript(function(callback) {
    var callback = arguments[arguments.length - 1];
    var myService = angular.injector(['ng']).get('myService');
    myService.async().then(function (d) {
            $scope.data = d;
    });
    callback(null, true);
    });
})

当我运行脚本时,包含angular.injector([' ng'])。get(' myService')的行会导致此错误:

UnknownError: javascript error: [$injector:unpr] Unknown provider: myServiceProvider <- myService

什么是正确的注射器声明?

1 个答案:

答案 0 :(得分:0)

尝试这个,

var injector = angular.injector(['app', 'ng']);
var myService = injector.get('myService');