1

I am trying to unit test the following code using jasmine but I can't seem to mock the checkStatus function. What am I seem to get wrong? It seems like when i actually call the Booking.saveBooking method it is not using the mocked version of the checkStatus. Please help.

Booking.js

const checkStatus = (id) => {
  .. // some code here
        
  return new Promise((resolve, reject)=>{
                resolve(value);
         });
}

const saveBooking =(req) => {
   checkStatus(req.id).then(()=>{
      //save booking here ..
   }).catch((error)=>{
      throw new Error();
      });
 }
    
 module.exports ={saveBooking, checkStatus}

booking.Spec.js

const Booking = require('Booking');

describe('Booking',()=>{

    const req = {
        id: 2133,
        customer_name: 'John Smith',
        contact_number: '888-8888',
        contact_email: 'cam888@example.com'
      };


 it('Should check the status', async ()=>{

    spyOn(Booking, "checkStatus").and.callFake(function() {
        var deferred = $q.defer();
        deferred.resolve(true);
        return deferred.promise;
    });
  
    await Booking.saveBooking(req);
    expect(Booking.checkStatus).toHaveBeenCalled()

  });
});

I am getting an error Error: Expected spy checkStatus to have been called.

It seems like saveBooking method it is not using the mocked version of the checkStatus.

cam
  • 129
  • 1
  • 12
  • 1
    https://stackoverflow.com/questions/45111198/how-to-mock-functions-in-the-same-module-using-jest/70066090#70066090 (except instead of not mocking it in Jest, you don't mock it in Jasmine). – jonrsharpe Nov 23 '21 at 00:33
  • i guess i can just not mock the checkStatus and let it call it as it is. Only mock functions that are being imported. – cam Nov 23 '21 at 08:34
  • I think this is a good lesson to learn on how to structure your code so it will be more testable. – cam Nov 23 '21 at 08:35

0 Answers0