【发布时间】:2019-07-06 08:23:07
【问题描述】:
我正在尝试测试使用window.location 的三元分支。
@Injectable()
export class ABCService {
private hostUrl = (window.location.host.indexOf('localhost') > -1) ? 'example.com' : window.location.host;
...
测试用例:
it('check alternate URL', () => {
const URL = 'http://example.org/postMethod';
service.getData().subscribe(data => {
expect(data.length).toBe(1);
});
const req = httpMock.expectOne(URL);
expect(req.request.url).toBe(URL);
req.flush({data:[1]});
});
得到错误:
Error: Expected one matching request for criteria "Match URL: http://example.org/postMethod", found none.
关于如何测试window.location.host 是否不是本地主机的任何想法。
试过(window as any).location = { ...window.location, host: 'http://example.org' };
仍然得到主机 == localhost:9876;
或
有什么办法可以覆盖 window.location.host 的模拟值吗?
【问题讨论】:
标签: javascript angular unit-testing karma-jasmine