I am new to loopback 4. I wish to write unit test cases for my project. However, I could't found proper example where they have used mocking service and using stubs.
Please assist me or share any example of properly written unit test(using mock service and stubs).
Thank You
Below is my code base:
Controller
export class CaptureController {
constructor(
@inject(RestBindings.Http.RESPONSE) private response: Response,
@service(HelperService) private helperService: HelperService,
) { }
@post('/capture', {
'x-controller-name': CaptureSwagger.controller,
summary: CaptureSwagger.summary,
description: CaptureSwagger.description,
responses: {
'200': CaptureSuccess.response,
'400': CommonHttpErrors(Messages.badRequest, 400, ''),
'401': CommonHttpErrors(
Messages.unauthorized,
401,
Messages.unauthorized,
),
},
})
async capture(
@requestBody() body: Capture,
): Promise<Response> {
const response = await this.helperService
.getGatewayService()
.capture(body);
const data = this.helperService.getGatewayKeys(
response,
TransactionType.COMPLETE,
);
const buildResponse = BuildResponse.prepare({data});
return this.response.json(buildResponse);
}
}
HelperService
@injectable({scope: BindingScope.TRANSIENT})
export class HelperService {
constructor(
@inject(RestBindings.Http.CONTEXT) private context: RequestContext,
@service(ConvergeService) private convergeService: ConvergeService,
) { }
getGatewayService() {
const merchantGateway: MerchantGateway =
this.context.getSync('merchantGateway');
const gatewayName: PaymentGateway | string = merchantGateway?.sysName
? merchantGateway.sysName
: '';
switch (gatewayName.toLowerCase()) {
case PaymentGateway.CONVERGE:
return this.convergeService;
default:
return this.nexioService;
}
}
}
ConvergeService
@injectable({scope: BindingScope.TRANSIENT})
export class ConvergeService {
/**
* Capture
* @param body Capture payment
*/
public capture = async (body: Capture) => {
const startAt = process.hrtime();
let errorResp = '';
const captureXml = `${this.xmlData}<ssl_transaction_type>cccomplete</ssl_transaction_type>
<ssl_amount>${body.data.amount}</ssl_amount>
<ssl_txn_id>${body.id}</ssl_txn_id>
</txn>`;
try {
const resp: {data: string} = await axiosApi.post(
`${this.convergeObj.apiUrl}`,
captureXml,
);
const jsonResp = this.parseXmlToJson(resp.data);
if (jsonResp?.txn?.errorCode) {
errorResp = resp.data;
throw new ResponseError.BadRequest(jsonResp.txn.errorMessage);
}
return jsonResp.txn ? jsonResp.txn : jsonResp;
} catch (err) {
this.commonService.getErrorResponse(err);
}
};
}