TypeError: Nie można odczytać właściwości „then” niezdefiniowanego testu angularjs-grunt


Używam usługi $ q do wykonywania wywołań asynchronicznych. Nie mogę rozstrzygnąć „wtedy” i „odroczenia” w testach jednostkowych za pomocą karmy.
Poniżej znajduje się mój kod kontrolera.
scope.filterUrls = [{url:'page1'}, {url: 'page2'}, {url:'page-error'}];scope.bindFilters = function () {
angular.forEach(scope.filterUrls, function (data) {
scope.getFilterData(data.url, '').then(function (result) {
if (data.url === 'page1') {
scope.moduleData.index = result.data;
} else if (data.url === 'page2') {
scope.moduleData.page2 = result.data;
}
});
});
}scope.getFilterData = function (filterUrls, params) {
// $q service object
var deferred = q.defer();// regular ajax request
http({
method: 'GET',
url: app.api.root + filterUrls,
params: params
})
.success(function (result) {
// promise resolve
deferred.resolve(result);
})
.error(function (result) {
// called asynchronously if an error occurs
// or server returns response with an error status.
deferred.reject('Erreur request : ' + result);
});
return deferred.promise;
};

specyfikacja testu:
it('should call getFilterData() in bindFilters()', function () {
spyOn(scope, 'getFilterData');
scope.bindFilters();
expect(scope.getFilterData).toHaveBeenCalled();
});

Otrzymuję błąd o nazwie "TypeError: niezdolny do odczytu" wtedy "niezdefiniowaną właściwość.
Jak mogę napisać test modułowy dla tych dwóch metod za pomocą karmy?
Aktualizacja:
1. jak możemy przetestować powodzenie i błąd metody scope.getFilterData ()
2. Następnie funkcja w scope.bindFilters ().
Proszę pomóż..
Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Jeśli potrzebujesz

tylko

odkryć,

czy wywoływana jest
getFilterData
, czy nie
, spróbuj zwrócić fałszywą obietnicę poprzez sfałszowanie funkcji:
W przypadku jasmine 1.3 moglibyśmy użyć
andCallFake
:
it('should call getFilterData() in bindFilters()', function () {
spyOn(scope, 'getFilterData').andCallFake(function(){//replace with a fake function
var deferred = $q.defer();//assume that you already inject $q service in beforeEach and save it as a variable.
return deferred.promise;//returns a fake promise
});
scope.bindFilters();
expect(scope.getFilterData).toHaveBeenCalled();
});

Od wersji Jasmine 2.0 moglibyśmy zamiast tego użyć
and.callFake
.
Kolejną decyzją jest użycie
AndreTurnValue
i
$ Q.Phen ()
:
it('should call getFilterData() in bindFilters()', function () {
spyOn(scope, 'getFilterData').andReturnValue($q.when());
scope.bindFilters();
expect(scope.getFilterData).toHaveBeenCalled();
});

Od wersji Jasmine 2.0 moglibyśmy zamiast tego użyć
and.returnValue
.

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się