我想测试记录器是否正确递增(使用 Jest)。查看下面的代码意味着我想检查是否调用了增量(1)
(即 void 的内容myMethod
)。
// myScript.ts
import dd from 'datadog-metrics';
export class MyClass {
private bufferedMetricsLogger: dd.BufferedMetricsLogger;
constructor(bufferedMetricsLogger: dd.BufferedMetricsLogger) {
this.bufferedMetricsLogger = bufferedMetricsLogger;
}
public myMethod(myInput: String): void {
this.bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${myInput}`]); // (1)
}
}
// myScript.test.ts
import { mock, instance, verify } from 'ts-mockito';
import { BufferedMetricsLogger } from 'datadog-metrics';
import { myClass } from 'path/to/myScript';
describe('Testing MyClass', () => {
it('verify that myMethod increments', () => {
const mockedBufferedMetricsLogger: BufferedMetricsLogger = mock(BufferedMetricsLogger);
const bufferedMetricsLogger: BufferedMetricsLogger = instance(mockedBufferedMetricsLogger);
const mockMyClass: MyClass = mock(MyClass);
const myClassOption1: MyClass = instance(mockMyClass);
const myClassOption2: MyClass = new MyClass(mockedBufferedMetricsLogger);
myClassOption1.myMethod('XXX'); // Option 1
myClassOption2.myMethod('XXX'); // Option 2
bufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${XXX}`]); // Option 3
verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, [`myTag:${XXX}`])).once();
})
});
我尝试了三种不同Option
的 s。每个Option
结果都在消息中
Expected "increment(strictEqual(my.key.name)", strictEqual(1), strichtEqual([myTag:XXX]))" to be called 1 time(s). But has been called 0 time(s).
实际上要么Option 1
或Option 2
就是我想要的。因为Option 3
我只是尝试过,因为我认为这肯定会奏效。
有人可以帮我吗?
提前致谢。