单元测试AngularJS服务方法使用$ q.all解析多个promise

时间:2015-10-19 09:23:26

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

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

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

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

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

1 个答案:

答案 0 :(得分:0)

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

});

解决! :)