Я сделал следующий тест, используя MockAdapter из axios-mock-adapter
. Однако я пытаюсь утверждать, что функция get была эффективно вызвана, поэтому я создал шпиона. По какой-то причине это не работает, и я получаю:
expect(jest.fn()).toHaveBeenCalled()
Expected number of calls: >= 1
Received number of calls: 0
Вот мой тест:
it('gets publications', async() => {
let spy = jest.spyOn(axios, "get");
var mock = new MockAdapter(axios);
mock.onGet(PUBLICATIONS_PATH + '/publications').reply(200,
{
answer: {
publications: [ "pub1", "pub2", "pub3" ]
}
});
let queryParameters = {
operation: 'FSale'
}
const publications = await PublicationService.getPublications(queryParameters);
expect(publications.data.answer.publications).toEqual([ "pub1", "pub2", "pub3" ]); // works fine
expect(spy).toHaveBeenCalled(); //This fails
})
Я на самом деле пытался использовать подход ответ здесь .
Обновление: вот код для getPublications
async function _getPublications(queryParameters){
return await axios({
method: 'get',
url: `${PUBLICATIONS_PATH}/publications`,
cancelToken: CancelTokenService.getSource().token,
params: queryParameters,
headers: {
authorization: LocalStorageService.getAuthorization(),
'Accept': ResourcesVersions.PUBLICATION
}
}).then(function (response){ return response }).catch(function (error){ return (axios.isCancel(error) ? error : error.response) })
}