How to unit test angularjs controller with $location service

前端 未结 3 1685
离开以前
离开以前 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:16

    Why don't you simply use a spyOn function?

    describe('OpponentsCtrl', function() {
    
        var location;
    
        beforeEach(module(function($provide) {
            $provide.factory('OpponentsCtrl', function($location){
                location = $location;
            });
        }));
    
        it('should change location when setting it via show function', inject(function() {    
            spyOn(location, 'path');    
            expect(location.path).toHaveBeenCalledWith('/new/path');
        }));
    });
    

    Hope this helps!

提交回复
热议问题