2017-03-29 7 views
0

POST-EDIT:問題を解決したばかりです。私はすぐに私のソリューションを投稿しますが、誰かが正しい解決策を持っているなら私は彼らの答えを受け入れるでしょう。Angular 1.5.x/Jasmine - 呼び出されたと思われるスパイだとは言われませんでした。

アプリケーションを1.4から1.5に移行し、すべてのコントローラとディレクティブをコンポーネントに変更します。私は今、一度働かなかったというテストを持っています。私はいくつかの指導をしたいと思います。

私はサービスメソッドをスパイしようとしていますが、ユニットテストでは呼び出していません。これは、アプリケーションの実行時にAPI呼び出しが行われるためではありません。ここで私が受け取るメッセージは次のとおりです。

enter image description here

これは私のコンポーネントファイルである:ここ

(function(){ 
    "use strict"; 

    angular.module("app").component("profileComponent", { 
     templateUrl: "/templates/profile.component.html", 
     controllerAs: "vm", 
     bindings: { 
      resolvedUser: "<" 
     }, 
     controller: function(ImageService, $state){ 
      const vm = this; 
      const resolvedUser = this.resolvedUser; 

      resolvedUser ? vm.user = resolvedUser : $state.go("404"); 

      vm.$onInit = function(){ 
       ImageService.findByName(vm.user.pokemon.name) 
        .then(function(res){ 
         vm.user.pokemon.id = res.id; 
         vm.user.pokemon.image = res.sprites.front_default; 
         vm.user.pokemon.type = res.types[0].type.name; 
        }) 
        .catch(function(res){ 
         vm.user.pokemon.image = "https://www.native-instruments.com/forum/data/avatars/m/328/328352.jpg?1439377390"; 
        }); 
      } 

     } 
    }); 
})(); 

そして、私のspecファイルから関連する部分です。前述のように$のOnInitがspyOn後に呼び出される必要がある、

describe("profile.component", function(){ 
    var profileComponent, ImageService, $q, $httpBackend, $state, resolvedUser, jazzSpy, IS, 
     API = "http://pokeapi.co/api/v2/pokemon/"; 

    var RESPONSE_SUCCESS = // very large variable I've omitted for brevity. 

beforeEach(angular.mock.module("app")); 
    beforeEach(angular.mock.module("ui.router")); 

    beforeEach(inject(function(_ImageService_, _$q_, _$httpBackend_, _$state_, _$rootScope_){ 
     ImageService = _ImageService_; 
     $q = _$q_; 
     $httpBackend = _$httpBackend_; 
     $state = _$state_; 
     $rootScope = _$rootScope_; 
     $rootScope.$new(); 
    })); 

    describe("profileComponent with a valid user and valid Pokemon", function(){ 

     beforeEach(inject(function(_$componentController_){ 
      singleUser = { id: 2, name: "Erlich Bachman", email: "[email protected]", phone: 4155552233, pokemon: { isPresent: true, name: "celebi"}, icon: { isPresent: false, name: null} }; 
      let bindings = {resolvedUser: singleUser, ImageService: ImageService, $state: $state }; 
      profileComponent = _$componentController_("profileComponent", { $scope: {} }, bindings); 
      profileComponent.$onInit(); 
     })); 

      beforeEach(function(){ 
       spyOn(ImageService, "findByName").and.callThrough(); 
      }); 

      it("should set state to resolvedUser", function(){ 
       expect(profileComponent.user).toEqual(singleUser); 
      }); 

      it("should expect ImageService to be defined", function(){ 
       expect(ImageService.findByName).toBeDefined(); 
      }); 

      it("should call ImageService.findByName() and return Pokemon icon", function(){ 

        expect(profileComponent.user.pokemon.name).toEqual("celebi"); 

       $httpBackend.whenGET(API + "celebi").respond(200, $q.when(RESPONSE_SUCCESS)); 
       $httpBackend.flush(); 

       // This is where the test fails 
        expect(ImageService.findByName).toHaveBeenCalledWith("celebi"); 
      }); 
     }); 
+1

あなたは競合状態を持っている私は、テストが失敗しているコメントを作りました。 $ onInitはspyOnの前に呼び出されます。 – estus

+0

あなたは正しい答えを得ました! –

答えて

0

beforeEach(function(){ 
    spyOn(ImageService, "findByName").and.callThrough(); 
    profileComponent.$onInit(); 
}); 
関連する問題