Karma测试会在服务上抛出Provider not found错误

时间:2016-06-29 19:07:27

标签: angularjs unit-testing bdd karma-jasmine gulp-karma

我正在尝试创建一个简单的服务并在jasmine-karma环境中对其进行测试。

我使用过Angular 1.5.5

出于某种原因,我一直收到错误

Chrome 46.0.2490 (Windows 7 0.0.0) reddit api service should do something FAILED
    Error: [$injector:unpr] Unknown provider: redditServiceProvider <- redditService
    http://errors.angularjs.org/1.5.7/$injector/unpr?p0=redditServiceProvider%20%3C-%20redditService
        at c:/client/www/vendor/angular/angular.js:68:12
        at c:/client/www/vendor/angular/angular.js:4502:19
        at Object.getService [as get] (c:/client/www/vendor/angular/angular.js:4655:39)
        at c:/client/www/vendor/angular/angular.js:4507:45
        at getService (c:/client/www/vendor/angular/angular.js:4655:39)
        at injectionArgs (c:/client/www/vendor/angular/angular.js:4679:58)
        at Object.invoke (c:/client/www/vendor/angular/angular.js:4701:18)
        at Object.workFn (c:/client/node_modules/angular-mocks/angular-mocks.js:3078:20)
    Error: Declaration Location
        at window.inject.angular.mock.inject (c:/client/node_modules/angular-mocks/angular-mocks.js:3040:25)
        at Suite.<anonymous> (c:/client/www/app/mine/myspec.spec.js:12:16)
        at c:/client/www/app/mine/myspec.spec.js:7:3
        at c:/client/www/app/mine/myspec.spec.js:52:3
    TypeError: Cannot read property 'whenGET' of undefined
        at Object.<anonymous> (c:/client/www/app/mine/myspec.spec.js:18:18)
Chrome 46.0.2490 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.049 secs)
Chrome 46.0.2490 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.048 secs / 0.049 secs)
29 06 2016 13:03:43.349:DEBUG [karma]: Run complete, exiting.
29 06 2016 13:03:43.349:DEBUG [launcher]: Disconnecting all browsers
29 06 2016 13:03:43.650:DEBUG [launcher]: Process Chrome exited with code 0
29 06 2016 13:03:43.652:DEBUG [temp-dir]: Cleaning temp dir C:\Users\mine\AppData\Local\Temp\karma-78283773
29 06 2016 13:03:44.892:DEBUG [launcher]: Finished all browsers
[13:03:44] 'karma' errored after 15 s
[13:03:44] Error: 1

我的服务文件是:

 angular.module("reddit",[]).service("userService",
  function($http) {
    return {
      getSubredditsSubmittedToBy: function(user) {
        return $http.get("http://api.reddit.com/user/" + user + "/submitted.json").then(function(response) {
          var posts, subreddits;

          posts = response.data.data.children;

          // transform data to be only subreddit strings
          subreddits = posts.map(function(post) {
            return post.data.subreddit;
          });

          // de-dupe
          subreddits = subreddits.filter(function(element, position) {
            return subreddits.indexOf(element) === position;
          });

          return subreddits;
        });
      }
    };
  });

我的测试看起来像这样:

(function(){
  "use strict";

  describe("reddit api service", function () {
    var redditService, httpBackend;

    beforeEach(module("reddit"));

    beforeEach(inject(function (_redditService_, $httpBackend) {
      redditService = _redditService_;
      httpBackend = $httpBackend;
    }));

    it("should do something", function () {
      httpBackend.whenGET("http://api.reddit.com/user/yoitsnate/submitted.json").respond({
        data: {
          children: [
            {
              data: {
                subreddit: "golang"
              }
            },
            {
              data: {
                subreddit: "javascript"
              }
            },
            {
              data: {
                subreddit: "golang"
              }
            },
            {
              data: {
                subreddit: "javascript"
              }
            }
          ]
        }
      });
      redditService.getSubredditsSubmittedToBy("yoitsnate").then(function(subreddits) {
        expect(subreddits).toEqual(["golang", "javascript"]);
      });
      httpBackend.flush();
    });

  });

})();

我已按照本教程(http://nathanleclaire.com/blog/2014/04/12/unit-testing-services-in-angularjs-for-fun-and-for-profit/)创建此服务(而不是我复制了它) 我认为它确实有效,还有一些其他问题正在发生。我得到了一些工作代码,任何测试的服务都可以正常工作。 请让我知道我错在哪里。 谢谢!

1 个答案:

答案 0 :(得分:1)

问题在于您如何命名/注入Reddit服务。在您的服务文件中,您创建了一个名为userService的模块,但在测试中,您正在注入并使用一个名为redditService的模块,该模块并不是我所知道的。我认为如果你这样做会有效:

beforeEach(inject(function (_userService_, $httpBackend) {
  redditService = _userService_;
  httpBackend = $httpBackend;
}));