无法读取 angular 中未定义错误的 属性 'href'?

Cannot read property 'href' of undefined error in angular?

我正在尝试测试我的路由器功能。我收到此错误 无法读取未定义的 属性 'href' ..你能告诉我如何消除这个错误吗

这是我的代码 http://plnkr.co/edit/mZiNteMWqS4forycMHiK?p=preview

describe('router  check', function(){



  describe('router check',function(){
     var view = 'partials/state1.html',
     $state;

     var $scope;
   beforeEach(function(){
    module('app');
   });

   beforeEach(inject(function($rootScope,$templateCache,$state) {
     $state=$state;
     $templateCache.put(view, '');
        }));

         it('should map state state1 to url /state1 ', function() {
            expect($state.href('state1', {})).to.equal('/state1');
        });

        it('should map /state1 route to state1 View template', function () {
            expect($state.get('state1').templateUrl).to.equal(view);
        });


  })

问题是您没有在正确的范围内访问 $state。 beforeEach 中的语句 $state=$state 没有将状态分配给在 describe.

中声明的变量“$state”

尝试

describe('router check',function(){
 var view = 'partials/state1.html',
 $state_test;

 var $scope;


   beforeEach(function(){
    module('app');
   });

beforeEach(inject(function($rootScope,$templateCache,$state) {
         $state_test=$state;
         $templateCache.put(view, '');
            }));

您的代码几乎没有问题。我已修复它们并按预期工作。

Link 到 plunkr

describe('router  check', function(){
  describe('router check',function(){
     var view = 'partials/state1.html',
     $state, $templateCache, $scope;

   beforeEach(function(){
    module('app');

    inject(function(_$state_, _$templateCache_) {
      $state = _$state_;
      $templateCache = _$templateCache_;
    });

     $templateCache.put(view, '');
   });

    it('should map state state1 to url /state1 ', function() {
        expect($state.href('state1', {})).toEqual('#/state1');
    });

    it('should map /state1 route to state1 View template', function () {

      expect($state.get('state1').views.testView.templateUrl).toEqual(view);
    });
  });
});