代码之家  ›  专栏  ›  技术社区  ›  Danny908

Jasmine,自定义用户代理返回CORS来源错误

  •  0
  • Danny908  · 技术社区  · 7 年前

    我有一个测试,让userAgent检测设备是计算机还是移动设备,但当我运行测试时,我得到了CORS错误。

    这是测试代码:

      it('should detect if device is mobile', () => {
        // Custom user agent
        window.navigator['__defineGetter__']('userAgent', function() {
         return 'Mozilla/5.0 (Linux; Android 9; PH-1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36';
        });
        // component.getDevice();
        // fixture.detectChanges();
        const mobile = fixture.debugElement.query(By.css('div.dl-sidebar-backdrop.mobile'));
        expect(mobile).toBeTruthy();
      });
    

    这就是错误: enter image description here

    我做了一项研究,但没有发现任何与这个问题有关的信息。

    1 回复  |  直到 7 年前
        1
  •  0
  •   zmag    7 年前

    有一个XHR呼叫到某个不在 localhost 使用 HttpClient 在你的 SidebarComponent 我想是吧。删除该逻辑,然后重试。