鉴于以下测试
$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)组件:符合FicaStatusComponent且没有文档不符合FAILED 错误:意外请求: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;
}());
...
答案 0 :(得分:2)
您已在模块中添加了以下服务:
module("aureus").service("IFicaStatusService", FicaStatusService);
这意味着您需要IFicaStatusService
而不是ficaService
提供$provide
。