因果不明的提供者错误 [英] Karma unknown provider error

查看:95
本文介绍了因果不明的提供者错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全可以对javaScript,angularJS和Karma进行单元测试.我已经能够为控制器编写通过测试,但是现在尝试测试服务,则出现以下错误:Unknown provider <- nProvider <- User其中User是我的服务的名称,从我可以知道,"n"是该服务的$ http依赖项(最小).这是我的UserService(TypeScript):

I'm a total noob to unit testing javaScript, angularJS, and Karma. I've been able to write passing tests for the controllers, but now that I'm attempting to test the services, I'm getting the following error: Unknown provider <- nProvider <- User where User is the name of my service and from what I can tell, "n" is the $http dependency for that service (minified). Here is my UserService (TypeScript):

/// <reference path="../../lib/angular/angular.d.ts" />


module MyApp.Services{
    export class UserService{
        private _apiBase: string = "api/";

        constructor(private $http: ng.IHttpService, private $q: ng.IQService) {

        }

        getUser(userSearchParams: string): ng.IPromise{
            var deferred = this.$q.defer();
            this.$http.get(this._apiBase + "Users/" + userSearchParams).success((data) => {
                deferred.resolve(data);
            }).error(() => {
                deferred.reject();
            });

            return deferred.promise;
        }

    }
}

这是我的通用Services.ts文件:

and here is my generic Services.ts file:

/// <reference path="../_all.d.ts" />

'use strict'

var mod = angular.module('MyApp.Services', []);

mod.factory('User', [<any>"$http", <any>"$q", function ($http, $q) {
    return new MyApp.Services.UserService($http, $q);
}]);

最后,我的测试脚本:

'use strict';

describe('UserServiceTests', function () {

beforeEach(module('main'));  //this is my main angular module - set up in app.ts

it('should issue a GET request to api/Users/whatever when the search parameter is whatever', inject(function(User) {
    expect(2).toBe(2);
}));

})

我创建了另一个具有相同结构的测试应用程序,但没有得到上面看到的错误.我不知道我在想什么.让我知道是否还有什么需要发布才能发现问题.感谢您的帮助.

I've created another test app with the same structure and I don't get the error seen above. I don't know what I'm missing. Let me know if there is anything more that I need to post in order to find the problem. Any help is appreciated.

经过大量搜索和阅读,似乎这与依赖项注入有关.此处此处.缩小后,参数名称会更改,从而导致错误.作为证明,如果删除最小文件并重新运行测试,它们将通过.

After much searching and reading, it appears that this is related to dependency injection here and here. Upon minification, the parameter names get changed, thus causing the error. As proof, if I delete the min files and re-run the tests, they pass.

可以使用$ inject函数来解决此问题,但是当使用TypeScript将控制器作为类创建控制器时,我看不到如何做到这一点.我不确定是否可以使用$ routeProvider.when的resolve属性.

The $inject function can be used to fix this, but I don't see how to do it when the controller is created as a class using TypeScript. I'm not sure if I can use the resolve property of $routeProvider.when.

推荐答案

好,我发现

Ok, I found this post, which allowed me to resolve the issue. By adding an "inject" property to my TypeScript classes (with the appropriate values in the array), Angular knows how to handle the DI. The only drawback I see is the potential for error in the order the dependencies are listed, or omission in the event a new dependency is added at a later time.

这篇关于因果不明的提供者错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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