Mock document.referrer Angular Jasmine

I am trying to provide a different value for

document.referrer

for my unit tests. How can I mock him and provide an individual meaning?

+4
source share
1 answer

By checking document.referrer, we find that this is a getter: enter image description here

With Jasmine 2.6, you can create a spy to change the return value of any getter functions.

spyOnProperty(document, 'referrer', 'get').and.returnValue('http://foobar.com');
expect(document.referrer).toBe('http://foobar.com');

You can read more in the latest API documentation .

0
source

All Articles