我的代码:
this.apiStore.fetchOrder(id).then(() => {
alert("Success");
// TODO - ui updates
})
开玩笑:
describe("order", () => {
it("fetches the order", () => {
const fetchOrder = jest.fn();
const id = 100;
expect(fetchOrder).toHaveBeenCalledWith(id);
});
});
但是我遇到以下错误:
无法读取未定义的属性'then'
关于如何解决此问题的任何想法?
编辑1:
Fetchorder API调用的定义如下
fetchOrder = (orderId) => {
return this.fetch(
`/api/order.json`,
{
method: "POST",
body: {
id: orderId
},
}
);
};
目前,我以
返回响应{"status":"ok"}
如果我删除了.then() => {..}
,则测试运行正常。有什么想法吗?
编辑2:后端代码如下
def fetch_order
render json: { status: :ok }
end
答案 0 :(得分:-1)
fetchOrder函数不会返回Promise实例,您应该检查其实现或共享它。