How to fake being offline in Jasmine?
<p>I have a javascript function which is supposed to behave differently when offline than online as a safeguard.  I would like to have a Jasmine unit test which tests the function in both offline and online modes - e.g.,</p>

<pre><code>// offline
describe('When there is no connection to the internet', function() {
  beforeEach(function(){
    spyOn(navigator, 'onLine').and.returnValue(false);
  });

  it('offline behavior happens', function() {
    myFunction();

    expect(something).not.toHaveBeenCalled();
  });
});

// online
describe('When there is a connection to the internet', function() {
  beforeEach(function(){
    spyOn(navigator, 'onLine').and.returnValue(true);
  });

  it('online behavior happens', function() {
    myFunction();

    expect(something).toHaveBeenCalled();
  });
});
</code></pre>

<p>However, I am unable to fake the value of <code>navigator.onLine</code>.  In my <code>before</code>, I also tried:</p>

<pre><code>navigator = {
  'onLine': false
}
</code></pre>

<p>This didn't work either.  To be thorough, I tried the same techniques above with <code>window.navigator.onLine</code> and that also didn't work.  </p>

<p>Does anyone know how to mock being offline for a Jasmine test?</p>