2016-05-25 5 views
1

私は

storesController.js

(function() { 
    var app = angular.module('storesController', ['storesService']); 

    app.controller('StoresListController', function ($scope, StoresService) { 

     $scope.getStores = function() { 
      StoresService.getStores().then(function (data) { 
       $scope.stores = data.data; 
      }); 
     }; 
     $scope.getStores(); 

     $scope.deleteStore = function (id) { 
      StoresService.deleteStore(id).then(function() { 
       $scope.getStores(); 
      }); 

     }; 
    }); 
})() 

storesService.js

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

    app.factory('StoresService', ['$http','appConfig', function ($http,appConfig) { 

      var webServiceUrl = appConfig.webServiceUrl; 
      var stores = []; 

      stores.getStores = function() { 
       return $http.get(webServiceUrl + 'getStores'); 
      };    
      return stores; 

     }]); 

})(); 
、$ httpでいくつかのデータを取得するサービスを持つコントローラーをテストしようとしています

と私のテスト

describe("Store Controller", function() { 
var StoresService, createController, scope; 

beforeEach(function() { 
    module('storesController'); 
    module(function ($provide) { 
     $provide.value('StoresService', { 
      getStores: function() { 
       return { 
        then: function (callback) { 
         return callback([ 
          {name: "testName", country: "testCountry"}, 
          {name: "testName2", country: "testCountry2"}, 
          {name: "testName3", country: "testCountry3"}, 
         ]); 
        } 
       }; 
      }, 
     }); 
     return null; 
    }); 
}); 

beforeEach(function() { 
    inject(function ($controller, $rootScope, _StoresService_) { 
     scope = $rootScope.$new(); 
     StoresService = _StoresService_; 
     createController = function() { 
      return $controller("StoresListController", { 
       $scope: scope, 
      }); 
     }; 
    }); 
}); 

it("should call the store service to retrieve the store list", function() { 
    createController(); 
    expect(scope.stores.length).toBe(3); 
}); 

});

コントローラの作成時にメソッド$ scope.getStores()が呼び出され、変数$ scope.storesが長さ3のオブジェクトであることをテストしようとしています。 、それは私は、このエラー

TypeError: scope.stores is undefined 

たぶん私は$ httpBackendと異なるアプローチに行くべき

、私はユニットテストを始めているを取得し、それを動作させることができないと私は少し迷ってしまいました誰かが手を差し伸べることはできますか?

+0

$ HTTP GETリクエストがあります非同期であるため、リクエストが正しく機能している可能性がありますが、テストが実行されるとリクエストの結果は解決されません。 $ scope。$ apply()は助けになるかもしれませんが、角度のための単体テストを書く必要はありませんでした。 – sdemurjian

答えて

1

おそらくこれはコードの作業を行います。

return callback({ data: [ 
          {name: "testName", country: "testCountry"}, 
          {name: "testName2", country: "testCountry2"}, 
          {name: "testName3", country: "testCountry3"}, 
         ]}); 

しかし、私は実際に偽の約束は、より読みやすく、より少ないエラーが発生しやすくなり作成思う - このような何か:

describe("Store Controller", function() { 
    var StoresService, createController, scope, $q; 

    beforeEach(function() { 
     module('storesController'); 
     module(function ($provide) { 
      $provide.value('StoresService', { 
       getStores: function() {}, 
      }); 
      return null; 
     }); 
    }); 

    beforeEach(function() { 
     inject(function ($controller, $rootScope, _StoresService_, _$q_) { 
      $q = _$q_; 
      scope = $rootScope.$new(); 
      StoresService = _StoresService_; 
      createController = function() { 
       return $controller("StoresListController", { 
        $scope: scope, 
       }); 
      }; 
     }); 
    }); 

    it("should call the store service to retrieve the store list", function() { 
     var deferred = $q.defer(); 
     // Return fake promise. 
     spyOn(StoresService, 'getStores').and.returnValue(deferred.promise); 

     createController(); 
     // Resolve fake promise with some data. 
     deferred.resolve({ data: 'some data'}); 
     // Callback from promise wont execute without digest: 
     $rootScope.$digest(); 

     expect(scope.stores).toEqual('some data'); 
    }); 
    }); 
関連する問題