Angesichts der folgenden Test.
Wie stelle ich sicher, dass das Versprechen gelöst ist und die Daten bereitgestellt werden.Jasmin einen Schein Service in einem Winkel 1,5 Controller testen
describe("component: FicaStatusComponent",
function() {
var fs;
beforeEach(function() {
module("aureus",
function ($provide) {
$provide.service("ficaService", function() {
this.status = function() {
return $q(function (resolve, reject) {
resolve([{ documentType: { id: 1 } }]);
});
}
})
});
});
beforeEach(inject(function (_$componentController_, _ficaService_) {
$componentController = _$componentController_;
fs = _ficaService_;
}));
it("should expose a `fica` object", function() {
console.log('should expose');
var bindings = {};
var ctrl = $componentController("ficaStatus", null, bindings);
expect(ctrl.fica).toBeDefined();
});
it("compliant with no documents should not be compliant",
function() {
var ctrl = $componentController("ficaStatus");
expect(ctrl.fica.length).toEqual(1);
});
}
);
Der zweite Test konform ohne Dokumente ... versagt. Die Länge ist Null. Der andere Test wird übergeben, also habe ich den richtigen Controller instanziiert, die Eigenschaft ist definiert.
Der Scheindienst löst die Daten nicht korrekt auf, wahrscheinlich weil die Promise
noch ausgeführt wird oder gar nicht aufgerufen wird. Hier
ist die Implementierung des Controllers für die Komponente:
var FicaStatusController = (function() {
function FicaStatusController($log, $loc, ficaService) {
var _this = this;
this.$log = $log;
this.$loc = $loc;
this.ficaService = ficaService;
this.fica = [];
this.ficaService.status(1234).then(function (_) { return _this.fica = _; });
}
Der Service ist wie folgt:
var FicaStatusService = (function() {
function FicaStatusService($log, $http) {
this.$log = $log;
this.$http = $http;
}
FicaStatusService.prototype.status = function (accountNumber) {
var url = "api/fica/status/" + accountNumber;
this.$log.log("status: " + url);
return this.$http
.get(url)
.then(function (_) { return _.data; });
};
return FicaStatusService;
}());
...
Nicht $ scope. $ Digest. Es löst im Root-Bereich keinen Digest aus und führt keine Versprechungskette aus. – estus