英文:
How to mock stats method in elasticsearch?
问题
以下是要翻译的内容:
"我想使用Node.js查询Elasticsearch的统计信息。
这是我的源代码:
const { Client } = require('@elastic/elasticsearch');
client = new Client({
Connection: '适当的 AWS 连接',
node: '一些 URL',
agent: {
keepalive:,
maxsocket:,
maxfreesocket:
}
})
const stats = await client.nodes.stats();
console.log(stats);
// 统计信息上的一些操作
我如何模拟这个Elasticsearch方法?
我尝试了以下方法,但无法模拟stats()
。
let sandbox = sinon.createSandbox();
before(() => {
sandbox.stub(Client.prototype, 'nodes').get(() => {
stats: () => {
return dummydata;
}
// 其中dummydata已经硬编码
})
})
我在哪里出错了?"
英文:
I want to query stats of elasticsearch using nodeJs.
Here is my source code
const {Client} = require('@elastic/elasticsearch');
client = new Client({
Connection:'appropriate aws connection',
node:'some url',
agent:{
keepalive:,
maxsocket:,
maxfreesocket:
}
})
const stats = await client.nodes.stats();
console.log(stats);
// some operation on stats
How do i mock this elasticsearch method?<br>
I have tried following but it is unable to mock the stats()
.
let sandbox = sinon.createSandbox();
before(()=>{
sandbox.stub(Client.prototype,'nodes').get(()=>{
stats: ()=>{
return dymmydata;
}
//where dummydata is already hardcoded
})
})
Where am I going wrong here?
答案1
得分: 1
让我们看看nodes
API是如何定义的。请查看/v8.8.1/src/api/index.ts#L401,你会发现nodes
API是通过Object.defineProperties
和一个获取函数来定义的。
Object.defineProperties(API.prototype, {
// ...
nodes: {
get () { return this[kNodes] === null ? (this[kNodes] = new NodesApi(this.transport)) : this[kNodes] }
}
// ...
}
我们应该使用stub.get(getterFn)
API来替换这个存根的新获取函数。
例如:
index.js
:
const { Client } = require('@elastic/elasticsearch');
const client = new Client({
node: 'http://localhost:9200',
});
export function main() {
return client.nodes.stats();
}
index.test.js
:
const sinon = require('sinon');
const { Client } = require('@elastic/elasticsearch');
describe('76653113', () => {
it('should pass', async () => {
const nodesApiMock = {
stats: sinon.stub().resolves('ok'),
};
sinon.stub(Client.prototype, 'nodes').get(() => nodesApiMock);
const { main } = require('./');
const actual = await main();
sinon.assert.match(actual, 'ok');
sinon.assert.calledOnce(nodesApiMock.stats);
});
});
英文:
Let's see how the nodes
API is defined. See /v8.8.1/src/api/index.ts#L401, you will find the nodes
API is defined by Object.defineProperties
with a getter function.
Object.defineProperties(API.prototype, {
// ...
nodes: {
get () { return this[kNodes] === null ? (this[kNodes] = new NodesApi(this.transport)) : this[kNodes] }
}
// ...
}
We should use stub.get(getterFn)
API to replace a new getter for this stub.
E.g.
index.js
:
const { Client } = require('@elastic/elasticsearch');
const client = new Client({
node: 'http://localhost:9200',
});
export function main() {
return client.nodes.stats();
}
index.test.js
:
const sinon = require('sinon');
const { Client } = require('@elastic/elasticsearch');
describe('76653113', () => {
it('should pass', async () => {
const nodesApiMock = {
stats: sinon.stub().resolves('ok'),
};
sinon.stub(Client.prototype, 'nodes').get(() => nodesApiMock);
const { main } = require('./');
const actual = await main();
sinon.assert.match(actual, 'ok');
sinon.assert.calledOnce(nodesApiMock.stats);
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论