在下面的代码中,我的测试用例按预期方式通过了,但是我正在使用stryker进行突变测试,handleError函数在突变测试中仍然存在,因此我想通过测试是否调用handleError函数来杀死突变体。需要帮助测试私有功能。
我尝试了spyOn但没有用
const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
// code written in nestJS/typescript
export class OrderBuilder {
private amount: number
public withAmount(amount: number): BuyOrderBuilder {
this.amount = amount
return this
}
public build(): TransactionRequest {
this.handleError()
return {
amount: this.amount,
acceptedWarningRules: [
{
ruleNumber: 4464
}
]
}
}
private handleError() {
const errors: string[] = []
const dynamicFields: string[] = [
'amount',
]
dynamicFields.forEach((field: string) => {
if (!this[field]) {
errors.push(field)
}
})
if (errors.length > 0) {
const errorMessage = errors.join()
throw new Error(`missing ${errorMessage} field in order`)
}
}
}
// test
describe('Order Builder', () => {
it('should test the handleError', () => {
const orderBuilder = new OrderBuilder()
const errorMessage = new Error(
`missing amount field in order`
)
try {
orderBuilder.build()
} catch (error) {
expect(error).toEqual(errorMessage)
}
});
});
答案 0 :(得分:2)
您似乎想验证handleError
运行时是否build
被调用。
私有方法被编译为普通的JavaScript原型方法,因此您可以使用any
类型来让间谍创建通过TypeScript类型检查。
这是一个高度简化的示例:
class OrderBuilder {
public build() {
this.handleError()
}
private handleError() {
throw new Error('missing ... field in order')
}
}
describe('Order Builder', () => {
it('should test the handleError', () => {
const handleErrorSpy = jest.spyOn(OrderBuilder.prototype as any, 'handleError');
const orderBuilder = new OrderBuilder()
expect(() => orderBuilder.build()).toThrow('missing ... field in order'); // Success!
expect(handleErrorSpy).toHaveBeenCalled(); // Success!
});
});