1
我有一個使用Angular資源進行API調用的工廠。然後我創建了一個名爲getAllResources的函數,並使用工廠來查詢我的所有數據。在我的控制器中,我使用$ promise.then來獲取我的數據並將其存儲在一個變量中。
這是咖啡的腳本。如何單元測試這個:
廠:
resource "/perm/api/account/:uid/svcs",
uid: "@uid"
,
query:
cache: true
isArray: true
method: "GET"
服務:
getAllResources: (uid) ->
factory.query(uid: uid)
Ctrl鍵:
model.getAllResources(scope.accountID).$promise.then ((response) ->
scope.allResources = response
resPromise.resolve()
), (error) ->
log.debug error
state.go "pageunavailable"
我想通過以下對此進行測試:
beforeEach:
var def, model, service;
service = $injector.get("Service");
model = {
getAllResources: function() {
def = q.defer();
return def.promise;
}
};
controller("Ctrl", {
$scope: scope,
model: model
}, service);
它():
fakeProfSvcsData = [{id:0,name:"zeon"},{id:1,name:"leo"}];
spyOn(model, "getAllResources").andCallThrough();
def.resolve(fakeProfSvcsData);
scope.$digest()
expect(scope.allResources).toEqual(fakeProfSvcsData);