how to mock a location.path in angular unit tests

后端 未结 1 1125
闹比i
闹比i 2021-01-16 04:56

http://blog.artlogic.com/2013/05/06/angularjs-best-practices-ive-been-doing-it-wrong-part-2-of-3/

I am testing a routing directive with location.path refs to templat

相关标签:
1条回答
  • 2021-01-16 05:24

    I created a fiddle which demonstrates mocking $location.

      app.controller('testcont', function($scope, $location) {
          $scope.path = $location.path();
      });
      ...
      beforeEach(inject(function($controller, $rootScope, $location){    
         scope = $rootScope.$new();
         spyOn($location, 'path').andReturn('Fake location');
         $controller('testcont', {$scope:scope});
      }));
      ...
      it('should spy on $location', function($location){
         expect(scope.path).toBe('Fake location');
      });
    

    However templates can be loaded by prepopulating Angular's $templateCache with the directive. Karma uses the ng-html2js-preprocessor. Could that help your problem?

    0 讨论(0)
提交回复
热议问题