-
Notifications
You must be signed in to change notification settings - Fork 166
Closed
Labels
type: questionQuestions about the usage of the library.Questions about the usage of the library.
Description
Hello all,
I have some issues in unit testing container.get(service), checkout the below code.
import * as _ from 'lodash';
import {Service, Inject, Container} from 'typedi';
import OTPService from '../otp/index';
@Service()
export default class DevicechangeService {
constructor(@Inject('devicechangeModel')private devicechangeModel, @Inject('otpModel')private otpModel, @Inject('logger')private logger,) {}
/**
*
* Service to change the device
*
* @method deviceChange
* @param {number} accountId
* @param {string} deviceId
* @param {number} mobile
* @return {object} resolves after sending otp
*/
public async deviceChange(accountId : number, deviceId : string, newdeviceId : string, created : number, mobile : number,) : Promise < any > {
try {
await this
.devicechangeModel
.addDeviceChangeDetails(accountId);
let OTPServiceInstance = Container.get(OTPService);
await this
.otpModel
.updateOTPtoInactiveForModule('DEVICE_CHANGE', accountId);
//get the new otp
//get the user details
//send the otp
let result = await OTPServiceInstance.sendOTP(serviceprovider, mobile, otpMessage, isd);
if (result instanceof Object) {
return result;
}
return true;
} else {
return {
field: 421,
message: errorCode('account').getMessage(421)
};
}
} catch (e) {
throw e;
}
}
}Here i am not able to set mock class for OTPService, so not able to alter container.get(OTPService) from the above code.
I have tried below but still it does'nt come inside mocked service.
@Service()
class OTPService {
public generateOTP : any = jest
.fn()
.mockImplementation(async(accountId : number, deviceId : string, status : string) => new Promise((resolve : any) => {
console.log('calls here')
resolve({})
}),);
public sendOTP : any = jest
.fn()
.mockImplementation(async(serviceprovider : string, mobile : number, otpMessage : string, isd : number) => new Promise((resolve : any) => resolve({})),);
}
describe('Account', () => {
beforeEach(done => {
Container.set(OTPService, new OTPService());
done();
});
describe('#deviceChange', () => {
it('check everyting cool', async done => {
let logger = {};
let otpModel = {
updateOTPtoInactiveForModule: jest
.fn()
.mockImplementation(async(deviceChange : string, accountId : number) => new Promise((resolve : any) => resolve(true))),
getUserPhoneService: jest
.fn()
.mockResolvedValue(true)
};
let devicechangeModel = {
addDeviceChangeDetails: jest
.fn()
.mockResolvedValue(true),
updateDeviceChangeToInactive: jest
.fn()
.mockResolvedValue(true)
};
// Container.set('OTPService', new OTPService());
let DeviceChangeServiceInstance = new DeviceChangeService(devicechangeModel, otpModel, logger);
let deviceChangeStatus = DeviceChangeServiceInstance.deviceChange(accountId, deviceId, newdeviceId, created, mobile);
});
});
});Guys any input how to solve this...
Metadata
Metadata
Assignees
Labels
type: questionQuestions about the usage of the library.Questions about the usage of the library.