AngularJS和UI-Router:在angularjs单元测试期间'错误:转换被取代'

Sar*_*rah 19 javascript unit-testing angularjs angular-ui-router

我正在尝试为我的项目创建授权包.我在单元测试中得到错误'转换被取代',我无法找到实际上的位置.

单元测试:

import angular from 'angular';
import 'angular-mocks';
import worldManagerApp from '../../src/world-manager-app';
import security from '../../src/security/security';

const {inject, module} = angular.mock;

describe('LoginService', ()=> {
    let $httpBackend;
    let $rootScope;
    let successHandler;
    let errorHandler;
    let LoginService;

    const USER = {username: "TEST", password: "PASSWORD"};

    beforeEach(function() {
        module(worldManagerApp);
        module(security);

    });

    beforeEach(inject((_$httpBackend_, _LoginService_, _$rootScope_) => {
        $httpBackend = _$httpBackend_;
        $rootScope = _$rootScope_;
        LoginService = _LoginService_;

        successHandler = jasmine.createSpy('successHandler');
        errorHandler = jasmine.createSpy('errorHandler');
    }));

    it('should exist', () => {
        expect(LoginService).toBeDefined();
    });

    describe('.login()', () => {

        describe('when given a proper username and password', () => {
           it('should return the username and success', () => {
               $httpBackend.expectPOST('/login').respond(200, {user: USER});
               LoginService.login("TEST", "PASSWORD");
               $rootScope.$digest();
               $httpBackend.flush();

               expect($rootScope.currentUser).toEqual("TEST");
           });
        });
    });

});
Run Code Online (Sandbox Code Playgroud)

服务:

export default function LoginService($http){
    'ngInject';

    let service = {};
    service.login = login;

    function login(username, password){
        $http({
            url:'/login',
            method: 'POST',
            data: {
                username: username,
                password: password,
            },
        }).then (function(response) {
            response.username;
        }).catch(function(response){
        });
    }

    return service;
}
Run Code Online (Sandbox Code Playgroud)

错误:

PhantomJS 1.9.8 (Windows 8 0.0.0) LoginService .login() when given a proper username and password should return the username and success FAILED
        Error: transition superseded
            at C:/Users/Manifest/AppData/Local/Temp/353229d8bf0abe298e7003bab30c0528.browserify:9387 <- node_modules/angular-mocks/angular-mocks.js:261:0
            at processChecks (C:/Users/Manifest/AppData/Local/Temp/353229d8bf0abe298e7003bab30c0528.browserify:33750 <- node_modules/angular/angular.js:16674:0)
            at C:/Users/Manifest/AppData/Local/Temp/353229d8bf0abe298e7003bab30c0528.browserify:35048 <- node_modules/angular/angular.js:17972:0
            at C:/Users/Manifest/AppData/Local/Temp/353229d8bf0abe298e7003bab30c0528.browserify:34862 <- node_modules/angular/angular.js:17786:0
            at C:/Users/Manifest/AppData/Local/Temp/353229d8bf0abe298e7003bab30c0528.browserify:521 <- frontend\test\security\loginService.spec.js:42:15
Run Code Online (Sandbox Code Playgroud)

我认为这是一个ui-Router问题,但如果我做错了,我无法弄清楚它应该如何工作.

RWA*_*WAM 35

更新角度从1.5.9到版本1.6.1之后我遇到了同样的问题,可以通过更新依赖关系解决它:

npm update angular-ui-router
Run Code Online (Sandbox Code Playgroud)

旧版本0.3.1导致错误,更新版本0.3.2我的应用程序再次正常工作(现在角度为1.6.1).


小智 25

更新角1.6.1是这样的消息,但升级ui-router0.3.2解决问题


Cen*_*ael 7

这可能是您的路由逻辑错误.如果没有,请尝试将此添加到您的app.config() ;

$qProvider.errorOnUnhandledRejections(false)

  • 这对我有用.我将angularjs更新到版本1.6.4,并开始获得错误"转换已被取代".然后我将ui-router更新到版本0.3.1,我仍然遇到错误.然后将这个$ qProvider.errorOnUnhandledRejections(false)添加到我的app.config([$ qProvider] ......现在我没有收到任何错误.但问题仍然存在,**这样可以解决问题吗?**谢谢@Cengkuru Michael (2认同)