mercredi 29 juin 2016

Karma test throws a Provider not found error on a service

I am trying to create a simple service and test it in jasmine-karma environment.

I have used Angular 1.5.5

For some reason I keep getting a error

Chrome 46.0.2490 (Windows 7 0.0.0) reddit api service should do something FAILED
    Error: [$injector:unpr] Unknown provider: redditServiceProvider <- redditService
    http://ift.tt/299URmO
        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

My service file is:

 angular.module("reddit",[]).service("userService",
  function($http) {
    return {
      getSubredditsSubmittedToBy: function(user) {
        return $http.get("http://ift.tt/2951UdN" + 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;
        });
      }
    };
  });

My test looks like this:

(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://ift.tt/299UZCX").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();
    });

  });

})();

I have followed this tutorial(http://ift.tt/1qt1OOe) to create this service(rather I copied it) I assume that it does work and there is some other issue going on. I got some one elses working code and any service tested works fine. Please let me know where I am wrong. Thanks!

Aucun commentaire:

Enregistrer un commentaire