【问题标题】:how to unit test that an Angular controller has changed the route如何对 Angular 控制器更改路由进行单元测试
【发布时间】:2014-07-16 22:01:28
【问题描述】:

我试图弄清楚如何测试我的控制器逻辑来处理更改路由,但即使代码在浏览器中工作,当测试运行代码时它似乎并没有改变路由。

这是我的控制器的简化版本

myApp.controller('locationController', ['$location',
    function($location) {
        'use strict';
        $location.path('/dashboard');
    }
]);

最简单的测试级别,只检查控制器是否已加载以及路由是否从最初设置的内容更改为其他内容

describe('Testing c-app-controller logic', function() {
    var ctrl,
        $scope,
        $rootScope,
        location,
        route;

    beforeEach(function() {
        module('optApp');
        inject(function (_$rootScope_, $controller, _api_, _$location_, $httpBackend, $route) {
            $scope = $rootScope.$new();
            $rootScope = _$rootScope_;
            location = _$location_;
            route = $route;
            ctrl = $controller('locationController', {
                $scope: $scope,
                $rootScope: _$rootScope_,
                location: _$location_,
                route: $route
            });
        });
    });

    describe('location controller', function () {
        it('should change location', function () {
            location.path('/login');
            expect(route.current.controller).toBe('locationController');
            $rootScope.$digest();
            expect(location.path()).toBe('/dashboard');
        });
    });

});

这是我得到的:

Expected '/login' to be '/dashboard'.
Error: Expected '/login' to be '/dashboard'.

【问题讨论】:

    标签: angularjs unit-testing jasmine karma-runner


    【解决方案1】:

    嗯,很奇怪,我似乎找到了让它工作的方法。 Angular 摘要如何在测试中运行的一些奇怪的东西,但是如果我将代码包装在一个函数中并从测试中调用该函数,我可以获得更改路径并且测试成功。

    控制器

    myApp.controller('locationController', ['$location',
        function($location) {
            'use strict';
            $scope.changeLocation = function(){
                $location.path('/dashboard');
            }
        }
    ]);
    

    测试

    describe('Testing c-app-controller logic', function() {
        var ctrl,
            $scope,
            $rootScope,
            location,
            route;
    
        beforeEach(function() {
            module('optApp');
            inject(function (_$rootScope_, $controller, _api_, _$location_, $httpBackend, $route) {
                $scope = $rootScope.$new();
                $rootScope = _$rootScope_;
                location = _$location_;
                route = $route;
                ctrl = $controller('locationController', {
                    $scope: $scope,
                    $rootScope: _$rootScope_,
                    location: _$location_,
                    route: $route
                });
            });
        });
    
        describe('location controller', function () {
            it('should change location', function () {
                location.path('/login');
                expect(route.current.controller).toBe('locationController');
                $scope.changeLocation();
                $rootScope.$digest();
                expect(location.path()).toBe('/dashboard');
            });
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-16
      • 2017-06-22
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 2018-09-19
      相关资源
      最近更新 更多