Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock document.referrer Angular Jasmine

I am trying to provide a different value for

document.referrer

for my unit tests. How can I mock it and provide a customized value?

like image 771
Dribel Avatar asked Apr 12 '16 08:04

Dribel


1 Answers

Inspecting document.referrer, we find that it 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.

like image 128
Chuanqi Sun Avatar answered Oct 31 '22 22:10

Chuanqi Sun