【问题标题】:Unit testing AngularJS service method thet resolves multiple promises with $q.all单元测试 AngularJS 服务方法使用 $q.all 解决多个承诺
【发布时间】:2015-10-19 09:23:25
【问题描述】:

我正在尝试测试以下服务方法,该方法调用另一个方法以使用不同的参数多次检索数据。

我只测试过一次解决一个承诺,所以我不确定如何将此方法作为业力/茉莉花单元测试:

getMappings(dealerId){

    let dfd = this.$q.defer();
    let promises = [];

    promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.DEALER, dealerId));
    promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.DAYCOUNT, dealerId));
    promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.FREQUENCY, dealerId));
    promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.RATE_STATIC, dealerId));
    promises.push(this.DataService.getTranslationMappings(this.TRANSLATIONS.SETTLEMENT_TYPE, dealerId));

    this.$q.all(promises)
    .then((resp) => {
        dfd.resolve({
            dealers: resp[0].data,
            daycounts: resp[1].data,
            frequencies: resp[2].data,
            rateStatics: resp[3].data,
            settlementTypes: resp[4].data
        });
    }, (resp) => {
        dfd.reject(resp);
    });

    return dfd.promise;
}

任何帮助将不胜感激。谢谢:)

【问题讨论】:

    标签: angularjs unit-testing jasmine karma-runner karma-jasmine


    【解决方案1】:
    it('should get the mappings for listing', () => {
    
        let dealerId = 1;
        let resp;
        let count = 0;
        let dealers = [
            {
                "id": 1,
                "dealerId": 1,
                "translation": {
                    "id": 2,
                    "description": "Wayne Enterprise"
                },
                "synonym": "CTRPTYCODE 1"
            }
        ];
        let daycount = [
            {
                "id": 1,
                "dealerId": 1,
                "translation": {
                    "id": 1,
                    "description": "DAYCT 1"
                },
                "synonym": "DAYCTCODE 1"
            }
        ];
        let frequency = [
            {
                "id": 1,
                "dealerId": 1,
                "translation": {
                    "id": 1,
                    "description": "FREQ A"
                },
                "synonym": "FREQCODE 1"
            }
        ];
        let ratestat = [
            {
                "id": 1,
                "dealerId": 1,
                "translation": {
                    "id": 1,
                    "description": "RATESTAT 1"
                },
                "synonym": "RATESTATCODE 1"
            }
        ];
        let settlement = [
            {
                "id": 1,
                "dealerId": 1,
                "translation": {
                    "id": 1,
                    "description": "SETTYPE A"
                },
                "synonym": "SETTYPECODE 1",
                "$$hashKey": "object:221"
            }
        ];
    
        spyOn(service, 'getMappings').and.callThrough();
        spyOn(DataService, 'getTranslationMappings').and.callFake(function(){
    
            var defer = this.$q.defer();
            let data;
    
            switch(count) {
                case 0:
                    data = dealers;
                    break;
                case 1:
                    data = daycount;
                    break;
                case 2:
                    data = frequency;
                    break;
                case 3:
                    data = ratestat;
                    break;
                case 4:
                    data = settlement;
                    break;
            }
    
            defer.resolve({
                success: true,
                error: null,
                data: data
            });
    
            count++;
    
            return defer.promise;
        });
    
        service.getMappings(dealerId).then((data) => {
            resp = data;
        });
    
        scope.$digest();
    
        expect(service.getMappings).toHaveBeenCalled();
        expect(service.getMappings).toHaveBeenCalledWith(dealerId);
        expect(DataService.getTranslationMappings).toHaveBeenCalled();
        expect(DataService.getTranslationMappings.calls.count()).toEqual(5);
        expect(DataService.getTranslationMappings.calls.argsFor(0)).toEqual(['DEALER', dealerId]);
        expect(DataService.getTranslationMappings.calls.argsFor(1)).toEqual(['DAYCOUNT', dealerId]);
        expect(DataService.getTranslationMappings.calls.argsFor(2)).toEqual(['FREQUENCY', dealerId]);
        expect(DataService.getTranslationMappings.calls.argsFor(3)).toEqual(['RATE_STATIC', dealerId]);
        expect(DataService.getTranslationMappings.calls.argsFor(4)).toEqual(['SETTLEMENT_TYPE', dealerId]);
        expect(resp.dealers).toEqual(dealers);
        expect(resp.daycounts).toEqual(daycount);
        expect(resp.frequencies).toEqual(frequency);
        expect(resp.rateStatics).toEqual(ratestat);
        expect(resp.settlementTypes).toEqual(settlement);
    
    });
    

    解决了! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 2019-09-01
      • 2015-01-01
      • 2013-02-09
      • 2014-03-05
      • 2015-03-14
      • 2014-05-19
      • 2016-10-06
      相关资源
      最近更新 更多