需要从规范文件中模拟方法objBBB.GetResult()来处理故障情况。由于它是通过AAA模块导入的,因此无法访问和模拟所需的结果。
我也总是得到“成功”的结果,因为没有调用我的模拟方法。请让我知道它可以解决此问题。
// Service code
import BBB = require('../data_layer/BBB_dl');
const objBBB: BBB.BBBDL = new BBB.BBBDL();
export default class AAA{
public async DoThings(event, callBack) {
....
let res = objBBB.GetResult();
if(res === 1){
...
return 'Success'
} else {
....
return 'Failed'
}
}
}
// Unit test code
import AAA from '../data_layer/AAA_bl';
import BBB = require('../data_layer/BBB_dl');
const objBBB: BBB.BBBDL = new BBB.BBBDL();
const AAAObj: AAA = new AAA();
it('scenario 2 test', async function () {
const expected: any = 'Failed';
objBBB.GetResult = jasmine.createSpy('objBBB.GetResult() spy')
.and.returnValue('Failed');
result = await AAAObj.DoThings(event, tempCallback);
expect(result).toEqual(expected);
});