AngularJS,路线解析,位置变更,单元测试,karma-jasmine

时间:2016-03-23 14:46:21

标签: angularjs unit-testing karma-jasmine

在角度我已经为所有动作写了路线决心,检查你是否登录

$routeProvider.when('/home', {
    resolve: {
        Authenticated: function($location, AuthAuthenticator, AuthIdentity) {
            return new Promise(function(resolve, reject) {
                AuthAuthenticator.init().then(
                    function( success ) {
                        if (!AuthAuthenticator.isAuthenticated() ) {
                            $location.path("/login");
                            resolve( true );
                        }
                        resolve( false );
                    },
                    function( error ) {
                        reject( error );
                    }
                );
            });
        }
    }
});

如果您尚未登录,我们会将您重定向到登录页面。现在我想在我们的业力 - 茉莉花单元测试中测试这个。但是如果我写测试,location.path就不会改变。

describe('LoginController', function() {
    beforeEach(module('dcApp'));

    beforeEach(function() {
        var _authenticated = false;

        AuthAuthenticatorMock = {
            isAuthenticated: function() {
                return _authenticated
            },

            setAuthenticated: function( authenticated ) {
                _authenticated = authenticated;
            },
        };

        module( function( $provide ) {
            $provide.value('AuthAuthenticator', AuthAuthenticatorMock);
        });
    });

    var $controller;

    beforeEach(inject(function( _$route_, _$location_, _$controller_, _AuthAuthenticator_ ){
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $route = _$route_;
        $location = _$location_;
        $controller = _$controller_;
        AuthAuthenticator = _AuthAuthenticator_;
    }));

    describe('Controller activation', function() {
        it ('redirects to login if user is not yet logged in', function() {
            AuthAuthenticator.setAuthenticated( false );
            var $scope = {};
            var controller = $controller('HomeController', { $scope: $scope });
            expect( $location.path() ).toBe('/login');
        });
    });

});

但结果是:

PhantomJS 2.1.1 (Linux 0.0.0) HomeController Controller activation redirects to login if user is not yet logged in FAILED
Expected '' to be '/login'.

现在我已经看到一些关于间谍的文档,但我不知道如何检查这个位置变化。

1 个答案:

答案 0 :(得分:0)

您可以在之前的eaches发生后自行调用解决方案来正确测试。

describe('LoginController', function() {
    beforeEach(module('dcApp'));

    beforeEach(function() {
        var _authenticated = false;

        AuthAuthenticatorMock = {
            isAuthenticated: function() {
                return _authenticated
            },

            setAuthenticated: function( authenticated ) {
                _authenticated = authenticated;
            },
        };

        module( function( $provide ) {
            $provide.value('AuthAuthenticator', AuthAuthenticatorMock);
        });
    });

    beforeEach( function() {
        var _path = '';

        locationMock = {
            path: function( argument ) {
                if (argument) {
                    _path = argument;
                }
                return _path;
            }
        };

        module( function( $provide ) {
            $provide.value('$location', locationMock );
        });
    });

    var $controller;

    beforeEach(inject(function( _$route_, _$location_, _$controller_, _AuthAuthenticator_ ){
        // The injector unwraps the underscores (_) from around the parameter names when matching
        $route = _$route_;
        $location = _$location_;
        $controller = _$controller_;
        AuthAuthenticator = _AuthAuthenticator_;
    }));

    describe('Controller activation', function() {
        it ('redirects to login if user is not yet logged in', function() {
            AuthAuthenticator.setAuthenticated( false );
            var resolveTest = $route.routes['/home'].resolve.Authenticated;
            $injector.invoke( resolveTest );
        // if it's a promise call apply
            $rootScope.$apply();
            expect( $location.path() ).toBe('/login');
        });
    });

});