简体   繁体   English

Karma / Jasmine Angular 1.6 $ q工厂发行

[英]Karma/Jasmine Angular 1.6 $q issues from factory

I'm trying to write a test for a spec file for an errorService, I keep getting timeouts on my spec for this one method only. 我正在尝试为errorService的规范文件编写测试,而我的规范始终仅对此一种方法超时。 I use other promise based methods in different .factory files, but for some reason this one is giving me problems getting the promise to resolve or reject. 我在不同的.factory文件中使用了其他基于promise的方法,但是由于某种原因,这使我难以解决或拒绝promise。 Neither seem to work. 似乎都不起作用。

FAILED TESTS:
errorService Test
Test errorService routeError functionality
  ✖ should test that a route error has return value of "error logged"
    Firefox 51.0.0 (Mac OS X 10.12.0)
  Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. in node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line 1973)
  attemptAsync/timeoutId<@node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1973:23

This is the .spec.js file that I'm attempting to run. 这是我正在尝试运行的.spec.js文件。 All the injects are due to me trying to figure out if I missed something being loaded or not being loaded. 所有注入都是由于我试图找出是否错过了正在加载的东西而导致的。

let httpInterceptor, errorService, $translate, $q, $uibModal, $location, $state;

beforeEach(module('core', function($translateProvider){
    $translateProvider.translations('en_US', {})
}));

beforeEach(inject(function ($injector) {
    httpInterceptor = $injector.get('httpInterceptor');
    errorService = $injector.get('errorService');
    $translate = $injector.get('$translate');
    $q = $injector.get('$q');
    $uibModal = $injector.get('$uibModal');
    $location = $injector.get('$location');
    $state = $injector.get('$state');
}));

describe('Test errorService routeError functionality', function () {
    it('should test that a route error has return value of "error logged"', function (done) {
        // const error = errorService.routeError(routeError);
        // expect(error).toBe('error logged');

        errorService.routeError(routeError)
            .then(function (res) {
                console.log('routeError unit test res', res);
                // expect(res).toBe('error logged');
                done();
            }, function (err) {
                console.log('routeError unit test err', err);
                // expect(err).toBe(null);
                done();
            })
    })

});

This is the service being referenced. 这是被引用的服务。

function errorService($q, $uibModal, $translate, $state){
    const service = {
        routeError: routeError,
        clientError: clientError,
        tunnelError: tunnelError
    };
    return service;

    function routeError(err) {
        // return 'error logged';
        return $q(function (resolve, reject) {
            console.error('ui-router error', err);
            reject('error logged');
        });
    }
}
angular.module('core')
    .factory('errorService', errorService);
errorService.$inject = ['$q', '$uibModal', '$translate', '$state'];

Apparently what I needed to do was setup a scope and run scope.digest() at the end of the it function. 显然,我需要做的是设置范围并在it函数的末尾运行scope.digest()。

eg 例如

beforeEach(inject(function($injector){
    errorService = $injector.get('errorService');
    httpInterceptor = $injector.get('httpInterceptor');
    scope = $injector.get('$rootScope').$new();
}));

and for the test itself. 和测试本身。

describe('Test errorService routeError functionality', function () {

    it('should test that a route error has return value of "error logged"', function (done) {
        errorService.routeError(routeError)
            .catch(function (err) {
                console.log('routeError unit test err', err);
                expect(err).toBe('error logged');
                done();
            });
        scope.$digest();
    })
});

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

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