开玩笑直到typemoq函数被调用

Cer*_*rIs 5 javascript jestjs enzyme typemoq

所以我有一个模拟(typemoq)http调用,我将其传递到我的react组件(安装了酶)中:

const mockhttp: TypeMoq.IMock<IHttpRequest> = TypeMoq.Mock.ofType<IHttpRequest>();
mockhttp
  .setup(x => x.get('/get-all-goal-questions'))
  .returns(() => {
    return Promise.resolve(mockResponse.object.data);
  });

const wrapper = mount(<Goals history={Object} http={mockhttp.object} />);

expect(wrapper.find('#foo')).to.have.lengthOf(1);
Run Code Online (Sandbox Code Playgroud)

但是,直到“期望”之后才调用模拟“ Get”,如何才能获得期望,直到调用该模拟进行测试?

//编辑这里是测试代码

let httpCall = this.props.pageHoc.httpRequest -- the call im mocking
Run Code Online (Sandbox Code Playgroud)

let httpCall = this.props.pageHoc.httpRequest -- the call im mocking
Run Code Online (Sandbox Code Playgroud)

小智 1

嗯,如果您尝试测试异步请求,您应该遵循此处编写的内容: https: //jestjs.io/docs/en/tutorial-async

对于简短版本,您的测试应该如下所示:

it('works with async/await', async () => {
  expect.assertions(1);
  const data = await user.getUserName(4);
  expect(data).toEqual('Mark');
});
Run Code Online (Sandbox Code Playgroud)