我需要找到改变userAgent价值的方法.我试过spyOn了 window.navigator.userAgent.但这没有帮助.
JS:
@Injectable()
export class DetectBrowserService {
browserIE: boolean;
constructor() {
this.browserIE = this.detectExplorer();
}
public detectExplorer() {
const brows = window.navigator.userAgent;
const msie = brows.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
规格:
it('should test window.navigator.userAgent', () => {
const wind = jasmine.createSpy('window.navigator.userAgent');
wind.and.returnValue('1111');
detectBrowserService = TestBed.get(DetectBrowserService);
console.log(window.navigator.userAgent);
});
Run Code Online (Sandbox Code Playgroud)
我期待1111,但得到了关于我的浏览器的真实信息.