在链式承诺上创造茉莉花间谍

时间:2015-04-19 13:39:04

标签: angularjs jasmine karma-jasmine

我想测试一个基于Restangular的服务。问题在于我找不到一种方法来创造关于链式承诺的正确间谍。

var services = angular.module('services.dashboards', ['models.dashboards', 'models.metrics', 'LocalStorageModule', 'app.filters']);

services.factory('DashboardsService', ['Restangular', '$q', 'Metrics', 'Dashboards', 'localStorageService', '$filter', 'filterByGradedFilter', function (Restangular, $q, Metrics, Dashboards, localStorageService, $filter, filterByGradedFilter) {
    var factory = {};

    factory.getData = function () {
        var defer = $q.defer();
        var data = localStorageService.get('data');
        if (!data) {
            Dashboards.post()
                .then(function (result) {
                    return Restangular.oneUrl('newDash', result.data).get();
                })
                .then(function (result) {
                    data = result.data;
                    localStorageService.set('data', data);
                    defer.resolve(data);
                });
        } else {
            defer.resolve(data);
        }
        return defer.promise;
    }

    return factory;

}]);

我想测试的链式承诺是:

            Dashboards.post()
                .then(function (result) {
                    return Restangular.oneUrl('newDash', result.data).get();
                })
                .then(function (result) {
                    data = result.data;
                    localStorageService.set('data', data);
                    defer.resolve(data);
                });

测试正在运行但我收到错误:Error: get() method does not exist

describe('Dashboards Service', function () {

    // Holds the service under test
    var service;
    // Dependencies
    var dashboards;
    var restangular;
    var q;
    var rootScope;

    beforeEach(module('services.dashboards'));

    // We inject the service
    beforeEach(inject(function (_DashboardsService_, _Dashboards_, _Restangular_, _$q_, _$rootScope_) {
        service = _DashboardsService_;
        dashboards = _Dashboards_;
        restangular = _Restangular_;
        q = _$q_;
        rootScope = _$rootScope_
    }));

    describe('#getData', function ($rootScope) {
        it('should get a dashboard', function () {
            spyOn(dashboards, 'post').and.returnValue(q.when({}));
            // Tried that but it's not working
            spyOn(restangular, 'oneUrl').and.callFake(function() {
                return this;
                // Also tried return restangular;
            });
            spyOn(restangular, 'get').and.returnValue(q.when({}));
            service.getData().then(function (result) {
                expect(result).toEqual({});
            });

            expect(dashboards.post).toHaveBeenCalled();
            expect(restangular.oneUrl).toHaveBeenCalledWith('newDash');
            expect(restangular.oneUrl.get).toHaveBeenCalled();
            rootScope.$apply();

        })
    })
})

1 个答案:

答案 0 :(得分:4)

你能试试吗

 spyOn(restangular, 'oneUrl').and.callFake(function() {
               return {
                    get:function() {return q.when({});}
               }
            });

基本上,您需要返回具有get方法的对象,该方法返回promise或数据。