0

I got a problem with mocking the location reload functionality within Jasmine. I tried several methods (method 1 , method 2) to mock any location reload events but with no luck.

My situation is the following thing. I have a rather simple function:

function TestCall(xhr) {
   if (xhr === 401) {       
        location.reload();
   }
}

I tried creating the following Jasmine tests:

it("FakeCall", function (){
    spyOn(TestCall, 'reload').and.callFake(function(){});
    TestCall(401);
    expect(TestCall).toHaveBeenCalled(); // this should check if reload functionality have been called 
}); 

I want to mock the location reload function but I have no clue why this does not work. Can anyone guide/tell me what I do wrong?

Total code:

describe("multiple scripts", function () {
    describe("2# FakeCall", function() {                            
        function TestCall(xhr) {            
            if (xhr === 401) {      
                location.reload();
            }
        }           

        it("2.1 # Reload", function (){                         
            spyOn(location, 'reload');
            TestCall(401);
            expect(location.reload).toHaveBeenCalled();                 
        }); 
    });  
});
Community
  • 1
  • 1
Rotan075
  • 2,567
  • 5
  • 32
  • 54

1 Answers1

0

When you use spyOn, you give object as the first argument and a name of its method (it is attribute of that object) as the second.

So instead of spyOn(TestCall, 'reload') use this spyOn(location, 'reload'). Now it should work.

In your case it could look like this

it("FakeCall", function (){
  spyOn(location, 'reload');
  TestCall(401);
  expect(location.reload).toHaveBeenCalled(); 
}); 
Dan Macak
  • 16,109
  • 3
  • 26
  • 43
  • Thanks for your explanation but how would that work within my example? Because I test it and the response I get is: `Expected a spy, but got file` – Rotan075 Jan 26 '16 at 13:54
  • 1
    Do you than verify it like `expect(location.reload).toHaveBeenCalled();` ? – Dan Macak Jan 26 '16 at 14:01
  • No, I did now but if I do that I get the following message: `Error: Expected a spy, but got Function.` Btw thanks for your help ;) – Rotan075 Jan 26 '16 at 14:11
  • You mean even if you try the piece of code I just added to my answer, it still doesn't work? Works for me in Jasmine + Karma in PhantomJS and Chrome. – Dan Macak Jan 26 '16 at 14:16
  • Yep, Look at my edited questions. I added the total test. I use Jasmine-standalone runner btw but that should not matter right? Could you check if my setting is the same as yours? – Rotan075 Jan 26 '16 at 14:25
  • 1
    I've run your code and it works. But you can try to use `window.location` instead of `location`. Additionally, I suggest you try [Karma](https://karma-runner.github.io/0.13/index.html) for your client-side code tests, as it gives you many benefits including possibility to test your code in multiple real browsers at once. – Dan Macak Jan 26 '16 at 14:54
  • For some reasons when I run it in PhantomJS directly via command line it does not break and the test succeeds. Fair enough. Still strange why it should not work within my chrome browser. One last question. Do you use a specific jQuery version? – Rotan075 Jan 26 '16 at 15:48
  • 1
    I've tried but I just don't get your Error in Chrome, strange. I avoid jQuery wherever possible, so I don't use it at all. – Dan Macak Jan 26 '16 at 18:50