业力 - 间谍功能错误处理

时间:2016-09-07 16:31:29

标签: javascript angularjs jasmine karma-jasmine

我有这样的服务:

app.service('usersService.v2', ['$http', '$q', '$exceptionHandler', 'User', 'userValidator.v2', 'CRUD', function($http, $q, $exceptionHandler, User, userValidator, CRUD){
    function dummyPromise(){
        var dummyDeferred = $q.defer();
        dummyDeferred.resolve('dummy');

        return deferred.promise;
    }

    this.getUser = function(userID, companyID){
        try{
            userValidator.validateId(userID);
            userValidator.validateId(companyID);
        }
        catch(e){
            $exceptionHandler(e);
            return dummyPromise();
        }

        return $http.get(apiUrl + 'api/v2/companies/' + companyID + '/users/' + userID)
            .then(function(response){
                var user = new User(response.data);

                try{
                    userValidator.validateUser(CRUD.READ, user);
                }
                catch(e){
                    $exceptionHandler(e);
                    return;
                }

                return user;
            })
    };
}]);

基本上我想根据验证函数的作用来测试此服务的行为。 userValidator.*函数是if / else阻止抛出错误。

在Karma,我有这样的事情:

describe('Service: usersService.v2', function () {
    var usersService, httpBackend, state, userValidator;
    const  url = 'address'

    function _inject() {
        angular.mock.inject(function ($injector) {
            usersService = $injector.get('usersService.v2');
            userValidator = $injector.get('userValidator.v2');
            httpBackend = $injector.get('$httpBackend');
        });
    }

    beforeEach(function () {
        angular.mock.module('app');
        _inject();
    });

    describe('getUser', function () {
        beforeEach(function () {
            httpBackend.when('GET', url);
        });
        afterEach(function () {
            httpBackend.verifyNoOutstandingExpectation();
            httpBackend.verifyNoOutstandingRequest();
        });

    it('should return a dummy promise if ID validation fails', function(){
            spyOn(userValidator, 'validateId').and.throwError('Missing or wrong ID thrown');
            usersService.getUser()
                 .then(function(data){expect(data).toBe('dummy');})
        });
    )};
})

但是当我运行Karma时,我得到一个错误,因为如果我没有放catch来处理预期,并且不执行下面的代码块。

我做错了什么?

干杯, 曼努埃尔

更新 验证方法是这样的:

... code code code ...
this.validateId = function(ID){
    if(!ID || !angular.isNumber(ID)) throw 'Missing or wrong ID';
}

因此我的问题是,Karma正在尝试处理验证引发的错误,而不是让userService执行此操作。

1 个答案:

答案 0 :(得分:0)

您正在测试usersService.v2。您无法同时测试userValidator.v2。但您可以模拟userValidator.v2服务。

var userValidator;
beforeEach(function() {

    module(function($provider) {
        userValidator = {
            validateId: function(id) {

                if (id === 123 ||id === 456 ) { //put your mock test id here for PASS case
                    return true
                }
                return false;
            }
        }
        $provider.value('userValidator.v2', userValidator);
    })
});
describe('getUser', function () {
        beforeEach(function () {
            httpBackend.when('GET', url)
            .respond(200, {
                    data: "dummy"
                });;
        }); 

    it('should return a dummy promise if ID validation fails', function() {

        usersService.getUser(9879,8798) //Its FAILED case
            .then(function(data) { expect(data).toBe('dummy'); })

    }) 
})