单元测试 newRelic.increment 指标
Unit Testing newRelic.increment Metric
我有一个调用 newrelic.incrementMetric()
的控制器,我想编写一个断言来检查它是否使用正确的
调用
我的控制器看起来像
const newrelic = require('newrelic')
async index() {
// some stuff here
newrelic.incrementMetric('hello', 1)
}
我在测试中试过这个
const newrelic = require('newrelic')
// describe block here...
it('should call newRelic', async () => {
newrelic.incremetentMetric = jest.fn().mockResolvedValueOnce({});
expect (newrelic.incrementMetric).toHaveBeenCalledWith('hello', 1)
});
执行此操作的正确方法是什么?
我的代码有错误
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function incrementMetric]
```
使用 jest.mock
工厂模拟 newrelic
。在测试用例中,您必须调用控制器的功能(A - 操作)。
index.ts
import newrelic from 'newrelic'
class Controller {
index() {
// some stuff here
newrelic.incrementMetric('hello', 1)
}
}
export default new Controller()
index.spec.ts
import newrelic from 'newrelic'
import controller from './index'
jest.mock('newrelic', () => {
return {
incrementMetric: jest.fn(),
}
})
describe('Controller', () => {
it('should call incrementMetric with correct', () => {
controller.index()
expect(newrelic.incrementMetric).toHaveBeenCalledWith('hello', 1)
})
})
我有一个调用 newrelic.incrementMetric()
的控制器,我想编写一个断言来检查它是否使用正确的
我的控制器看起来像
const newrelic = require('newrelic')
async index() {
// some stuff here
newrelic.incrementMetric('hello', 1)
}
我在测试中试过这个
const newrelic = require('newrelic')
// describe block here...
it('should call newRelic', async () => {
newrelic.incremetentMetric = jest.fn().mockResolvedValueOnce({});
expect (newrelic.incrementMetric).toHaveBeenCalledWith('hello', 1)
});
执行此操作的正确方法是什么?
我的代码有错误
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function incrementMetric]
```
使用 jest.mock
工厂模拟 newrelic
。在测试用例中,您必须调用控制器的功能(A - 操作)。
index.ts
import newrelic from 'newrelic'
class Controller {
index() {
// some stuff here
newrelic.incrementMetric('hello', 1)
}
}
export default new Controller()
index.spec.ts
import newrelic from 'newrelic'
import controller from './index'
jest.mock('newrelic', () => {
return {
incrementMetric: jest.fn(),
}
})
describe('Controller', () => {
it('should call incrementMetric with correct', () => {
controller.index()
expect(newrelic.incrementMetric).toHaveBeenCalledWith('hello', 1)
})
})