5

This is my controller code

$scope.loadApplications = function () {
      var cacheKey = { key: cacheKeys.appList() };
      dataFactory.get("/Application/All", { cache: cacheKey })
             .then(function (result) {
                 $scope.count++;                     
                 $scope.applications = result.data.data;
                 $scope.filteredApplicationsList = $scope.applications;
                  if ($scope.applications.length===0){
                     $window.location.href = '/Account/WelCome'
                 }
                 else {
                     $scope.isLoad = true;
                  }

             });
  };

and this is my jasmine test cases for above function

  it('should redirect to welcome page', function () {
        scope.applications = {};
        scope.loadApplications();
        expect(window.location.href).toContain('/Account/WelCome');
    });

but it is taking the current url of browser and it is raising an error as

Expected 'http://localhost:49363/Scripts/app/test/SpecRunner.html' to contain '/Account/WelCome'.

can anybody please tell me how to test the url?

Syed Rasheed
  • 559
  • 2
  • 10
  • 29
  • 1
    Possible duplicate of [mocking window.location.href in Javascript](http://stackoverflow.com/questions/4792281/mocking-window-location-href-in-javascript) – Youssef Gamil Aug 31 '16 at 12:33

2 Answers2

7

Better to use $windows service instead of window object, since its a global object. AngularJS always refer to it through the $window service, so it may be overridden, removed or mocked for testing.

describe('unit test for load application', function(){
  beforeEach(module('app', function ($provide) {
    $provide.value('$window', {
       location: {
         href: ''
       }
    });
  }));
  it('should redirect to welcome page', function ($window) {
     scope.applications = {};
     scope.loadApplications();
     expect($window.location.href).toContain('/Account/WelCome');
  });
});
Jobin S Kumar
  • 139
  • 1
  • 6
0

you should write it in this way.

it('should redirect to welcome page', function () {
        scope.applications = {};
        scope.loadApplications();

        browser.get('http://localhost:49363/Account/WelCome');
        expect(browser.getCurrentUrl()).toEqual('whateverbrowseryouarexpectingtobein');
        expect(browser.getCurrentUrl()).toContain('whateverbrowseryouarexpectingtobein');

        //expect(window.location.href).toContain('/Account/WelCome');
    });
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53