次のテストがあるとします。
$provided
サービスが注入されていません。私がカルマでテストをデバッグすると、提供されているサービスが実際のであり、のではないことがわかります。ジャスミンでコンポーネントサービスがテストに注入されていない
私が$provide.service...
を削除すると、エラー:[$ injector:unpr]不明なプロバイダ:ficaServiceProvider < - ficaServiceが表示されます。これはサービスが登録されていることを意味します。
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);
});
}
);
なしの文書に準拠して第二の試験...が失敗しています。
Chrome 56.0.2924 (Windows 10 0.0.0) component: FicaStatusComponent compliant with no documents should not be compliant FAILED Error: Unexpected request: GET api/fica/status/
空のオブジェクトを注入することを想定していますが、実際のサービスはそれにもかかわらずありますか?ここで
module("aureus", function($provide) {
$provide.value("ficaService", function() { return {}; });
$provide.service("ficaService", function() { return {}; });
});
コンポーネント用のコントローラの実装です:
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 = _; });
}
FicaStatusController.$inject = ["$log", "$location", "IFicaStatusService"];
module("aureus").component("ficaStatus", new FicaStatusComponent());
module("aureus").service("IFicaStatusService", FicaStatusService);
次のようにサービスがある:
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;
}());
...
私は$ componentControllerであまり仕事をしていませんが、サービスを適切に注入していないような角度のドキュメントを読むことから、 '$ componentController(" ficaStatus "、{ficaService:fs}、bindings);'そのトリックですか? – tehbeardedone
ダン、私は本当にあなたが何かにそこにいたと思った。いいえ...葉巻、試してくれてありがとう! – Jim
あなたが私に完全な実例を提供できるなら、私はそれを理解することができます。私はあなたがすでに投稿したものを使って作業を試みましたが、それを動作させることはできません。私の 'ctrl'オブジェクトには何も作用しません。 – tehbeardedone