Sven
Sven

Reputation: 6338

Mocking $window inside run block

The following code in my app is supposed to redirect to the authentication service. All my tests fail because of an initial page reload.

angular('app', [])
    .run(['$window', function($window) {
        $window.location = 'auth url' + '&redirect=' + $window.location.href;
    };

Test Error: Some of your tests did a full page reload!

How do I mock $window.location inside a .run block so that I can prevent the page reload?

Upvotes: 0

Views: 645

Answers (1)

Patrick
Patrick

Reputation: 6948

Configure $window in the $provide service in your jasmine test. For example:

beforeEach(module("app"), function ($provide) {
   //mock a $window and $window.location (since $window.location.href is used from mod.run)
   var $window = {};
   $window.location = {};

   //configure this value with the provider.
   $provide.value("$window", $window);
});

Any $window that is used within your module now will be replaced with your $window object.

Upvotes: 1

Related Questions