我想测试记录器是否正确增加(使用Jest)。查看下面的代码,这意味着我想检查增量(1)
(意味着空白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
。每个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
,我只是尝试了一下,因为我认为这肯定可以。
有人可以帮我吗?
谢谢。
答案 0 :(得分:1)
您的代码中的问题在于,您正在传递一个数组作为第三个参数,并且,如您在返回的消息中看到的那样,它正在尝试使用strictEqual(===)对其进行检查。您可以使用嘲笑提供的deepEqual
函数来获得所需的结果。
verify(mockedBufferedMetricsLogger.increment('my.key.name', 1, deepEqual([`myTag:${XXX}`]))).once();
并确保您正确地处理了参数,因为我不得不稍微更改代码以使其起作用。这是我完整的工作代码:
// myScript.ts
import { BufferedMetricsLogger } from 'datadog-metrics';
export class MyClass {
private bufferedMetricsLogger: BufferedMetricsLogger;
constructor(bufferedMetricsLogger: 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, deepEqual } from 'ts-mockito';
import { BufferedMetricsLogger } from 'datadog-metrics';
import { MyClass } from './myScript';
describe('Testing MyClass', () => {
it('verify that myMethod increments', () => {
const mockedBufferedMetricsLogger: BufferedMetricsLogger = mock(BufferedMetricsLogger);
const bufferedMetricsLogger: BufferedMetricsLogger = instance(mockedBufferedMetricsLogger);
const myClassOption1 = new MyClass(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, deepEqual([`myTag:XXX`]))).once();
})
});