mi codigo es el siguiente
//agency_controller.js import axios from 'axios'; export const getProducerNamesAndBillingPlan = ({ agencyId = '', onSuccess= (x) => x } = {} ) => { if(!!agencyId) { axios.get('/agency/' + agencyId) .then(response => onSuccess.call(this, response['data'])) .catch(error => console.error(error)) } } //agency_controller.spec.js import { getProducerNamesAndBillingPlan } from "../../../../app/javascript/packs/controllers/agencies_controller"; import axios from 'axios'; const mockAxiosPromise = (response) => { return new Promise((resolve, _reject) => { resolve({ status: 200, data: response}); }); } describe('#getProducerNamesAndBillingPlan', () => { ... it('calls the given onSuccess method if the request is successful', () => { spyOn(axios, 'get').and.callFake(() => { return mockAxiosPromise('foo') }) const mockMethod = (x) => console.log(x) spyOn(console.log, 'call') getProducerNamesAndBillingPlan({ agencyId: 1, onSuccess: mockMethod }) expect(console.log.call).toHaveBeenCalledWith('foo') }) })Puedo decir que el código funciona porque cuando ejecuto la prueba, 'foo' se registra en la consola. Sin embargo, la prueba sigue fallando:
#getProducerNamesAndBillingPlan calls the given onSucess method if the request is sucessful FAILED Expected spy call to have been called with [ 'foo' ] but it was never called. at UserContext.<anonymous> (spec/javascripts/packs/controllers/agencies_controller.spec.js:1:17348) Lo mismo sucede con expect(console.log).toHaveBeenCalledWith('foo') . ¿Estoy haciendo algo mal?
El método axios.get() devuelve una promesa, pero la función getProducerNamesAndBillingPlan no la devuelve. Lo llamas en el caso de prueba. Cuando el código ejecuta la declaración de expectativa, la promesa no se resuelve ni se rechaza, por lo que no se llamó a su método onSuccess antes de la aserción.
Use async/await en el caso de prueba para asegurarse de que la promesa se resuelva o rechace antes de la afirmación.
agency_controller.js :
import axios from 'axios'; export const getProducerNamesAndBillingPlan = ({ agencyId = '', onSuccess = (x) => x } = {}) => { if (!!agencyId) { return axios .get('/agency/' + agencyId) .then((response) => onSuccess.call(this, response['data'])) .catch((error) => console.error(error)); } }; agency_controller.spec.js :
import axios from 'axios'; import { getProducerNamesAndBillingPlan } from './agency_controller'; describe('#getProducerNamesAndBillingPlan', () => { it('calls the given onSuccess method if the request is successful', async () => { spyOn(axios, 'get').and.resolveTo({ status: 200, data: 'foo' }); const mockMethod = (x) => console.log(x); spyOn(console, 'log'); await getProducerNamesAndBillingPlan({ agencyId: 1, onSuccess: mockMethod }); expect(console.log).toHaveBeenCalledWith('foo'); }); });Resultado de la prueba:
Executing 1 defined specs... Running in random order... (seed: 00239) Test Suites & Specs: 1. #getProducerNamesAndBillingPlan ✔ calls the given onSuccess method if the request is successful (5ms) >> Done! Summary: 👊 Passed Suites: 1 of 1 Specs: 1 of 1 Expects: 1 (0 failures) Finished in 0.01 seconds