【发布时间】:2021-05-21 20:28:33
【问题描述】:
我正在尝试使用 sinon 存根对 chai 进行一些测试。问题是,我正在像这样存根我的 fetch 并履行我的诺言。
let fetchedStub;
beforeEach(() => {
fetchedStub = sinon.stub(global, 'fetch');
fetchedStub.resolves({ json: () => { body: 'json' } });
});
然后我正在测试我的数据是否正确返回
it('should return the JSON data from the promise', () => {
const result = search('test');
result.then((data) => {
expect(data).to.be.eql({ body: 'json' });
});
});
但我没有通过测试,而是得到了
TypeError: Cannot read property 'then' of undefined
我的承诺做错了吗?我想我需要一些光。
编辑:这是搜索功能。
export const search = (query) => {
fetch(`https://api.spotify.com/v1/search?q=${query}&type=artist`)
.then(data => data.json());
};
【问题讨论】:
-
请出示
search函数的代码 -
问题就在那里
标签: node.js chai sinon stub sinon-chai