我正在尝试使用具有单个异步方法的类从网页中获取 HTML。我使用 Typescript 3.4.3,请求承诺 4.2.4。import * as rp from 'request-promise';class HtmlFetcher { public uri: string; public html: string; public constructor(uri: string) { this.uri = uri; } public async fetch() { await rp(this.uri).then((html) => { this.html = html; }).catch((error) => { throw new Error('Unable to fetch the HTML page'); }); }}export { HtmlFetcher };我使用以下代码使用 Jest 24.8.0 测试我的课程。第 6 行的地址仅用于测试目的,我也尝试使用不同的 URI。import { HtmlFetcher } from './htmlFetcher.service';describe('Fetch HTML', () => { it('should fetch the HTMl at the given link', () => { const uri = 'http://help.websiteos.com/websiteos/example_of_a_simple_html_page.htm'; const fetcher = new HtmlFetcher(uri); fetcher.fetch(); expect(fetcher.html).toBeDefined(); });});我希望该html属性包含在调用 fetch() 方法后在给定地址处获取的 HTML 字符串。但是,测试代码失败,并记录fetcher.html为undefined. Typescript、Jest 和 request-promise 文档没有提供任何帮助。我究竟做错了什么?
添加回答
举报
0/150
提交
取消