-1

I am trying to test my router functionality .I am getting this error Cannot read property 'href' of undefined ..could you please tell me how i will remove this error

here is my code 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);
        });


  })
user5711656
  • 3,310
  • 4
  • 33
  • 70

2 Answers2

2

There were few issues with your code. I have fixed them and its working as expected.

Link to 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);
    });
  });
});
Subash
  • 7,098
  • 7
  • 44
  • 70
0

The problem is you aren't accessing $state in the right scope. The statement $state=$state in beforeEach doesn't assign the state to variable "$state" declared within describe.

Try

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, '');
            }));
Baski
  • 829
  • 8
  • 14