How to unit test angularjs controller with $location service

前端 未结 3 1677
离开以前
离开以前 2020-12-24 12:00

I am trying to create a simple unit test that tests my show function.

I get the following error:

TypeError: Object # has no method \'sh         


        
      
      
      
3条回答
  •  既然无缘
    2020-12-24 12:26

    This is how my test ended up working.

    describe('OpponentsCtrl', function() {
        var scope, rootScope, ctrl, location;
    
        beforeEach(inject(function($location, $rootScope, $controller) {
            location = $location;
            rootScope = $rootScope;
            scope = $rootScope.$new();
            ctrl = $controller(OpponentsCtrl, {$scope: scope});
        }));
    
        it('should change location when setting it via show function', function() {
            location.path('/new/path');
            rootScope.$apply();
            expect(location.path()).toBe('/new/path');
    
            // test whatever the service should do...
            scope.show('/test');
            expect(location.path()).toBe('/test');
    
        });
    });
    

提交回复
热议问题