简体   繁体   English

茉莉在角度1.5控制器中测试模拟服务

[英]jasmine testing a mock service in an angular 1.5 controller

Given the following test. 给出以下测试。
How do I ensure that the promise is resolved, and the data is provided . 我如何确保兑现承诺并提供数据。

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

The second test compliant with no documents... is failing. 没有文件的第二项测试失败。 The length is zero. 长度为零。 The other test is passing, so I have the correct controller being instantiated, the property is defined. 另一个测试通过了,因此我实例化了正确的控制器,该属性已定义。

The mock service is not resolving the data correctly, probably because the Promise is still executing, or not being called at all. 模拟服务无法正确解析数据,可能是因为Promise仍在执行中,或者根本没有被调用。

Here is the implementation of the controller for the component: 这是该组件的控制器的实现:

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 = _; });
    }

The service is as follows: 该服务如下:

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

...

First, u can use $q like: 首先,您可以像这样使用$ q:

this.status = function () {
    return $q.when([{ documentType: { id: 1 } }]);
}

Second, to resolve promise use $scope.$digest, $rootScope.$digest: 其次,使用$ scope。$ digest,$ rootScope。$ digest解决承诺。

var a = $q.when({test: 1});
expect(a.test === 1).toBe(false);
$rootScope.$digest();
expect(a.test === 1).toBe(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM