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と異なるアプローチに行くべき
、私はユニットテストを始めているを取得し、それを動作させることができないと私は少し迷ってしまいました誰かが手を差し伸べることはできますか?
$ HTTP GETリクエストがあります非同期であるため、リクエストが正しく機能している可能性がありますが、テストが実行されるとリクエストの結果は解決されません。 $ scope。$ apply()は助けになるかもしれませんが、角度のための単体テストを書く必要はありませんでした。 – sdemurjian