组件服务没有被注入茉莉花测试

component service not being injected into test in jasmine

给出以下测试。
$provided 服务未被注入。如果我在 karma 中调试测试,我可以看到所提供的服务是 真实服务不是模拟服务

真正奇怪的是,如果我删除 $provide.service...,我会得到一个错误错误:[$injector:unpr] Unknown provider: 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/

我也试过了,希望注入一个空对象,但是"real"服务还在吗?

        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;
}());

...

您已经像这样在您的模块中添加了您的服务:

module("aureus").service("IFicaStatusService", FicaStatusService);

这意味着您需要提供 IFicaStatusService 而不是 ficaService$provide