我有一个javascript函数,它应该在离线状态下比网上的行为有所不同,作为安全措施。我想有哪些测试功能在离线和在线模式的茉莉花单元测试 - 例如,如何伪造在茉莉花下线?
// 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();
});
});
然而,我无法伪造的navigator.onLine
值。在我的before
中,我也试过:
navigator = {
'onLine': false
}
这也没用。为了彻底,我尝试了以上与window.navigator.onLine
相同的技巧,但也没有奏效。
有没有人知道如何模拟茉莉花测试脱机?
如果您的行为取决于一个布尔值,为什么不注入价值? 'myFunction(onlineValue)' – Bart
因为那么我需要用参数更新每一个调用我的方法,我不想? – carmenism