我正在嘗試為我的專案開發測驗,并且我有一個檔案連接到 rabbitmq 并使用了一個佇列,但是我在想測驗它時遇到了問題
const amqp = require('amqplib/callback_api');
const rabbitConsumer = (io) => {
setTimeout(() => {
amqp.connect('amqp://rabbitmq', (error0, connection) => {
if (error0) {
throw error0;
}
connection.createChannel((error1, channel) => {
if (error1) {
throw error1;
}
const queue = 'message';
channel.assertQueue(queue, {
durable: false,
});
console.log(' [*] Waiting for message', queue);
channel.consume(
queue,
(data) => {
console.log(' [x] Received data:', data.content.toString('utf-8'));
io.emit('sendMessage', data.content.toString('utf-8'));
},
{
noAck: true,
}
);
});
});
}, 10000);
};
module.exports = rabbitConsumer;
可以測驗這個檔案嗎?我怎么能使用 JEST 或任何其他庫來做到這一點?
uj5u.com熱心網友回復:
您可以使用jest.spyOn(object, methodName)為amqp物件的方法創建模擬。
使用jest.useFakeTimers(implementation?: 'modern' | 'legacy')告訴 jest 使用setTimeout函式的假版本,這樣你就不需要等待真正的延遲時間。
使用jest.advanceTimersByTime(msT??oRun)來
呼叫此 API 時,所有計時器都會提前 msToRun 毫秒。所有已通過 setTimeout() 或 setInterval() 排隊并在此時間范圍內執行的未決“宏任務”都將被執行
例如
index.js:
const amqp = require('amqplib/callback_api');
const rabbitConsumer = (io) => {
setTimeout(() => {
amqp.connect('amqp://rabbitmq', (error0, connection) => {
if (error0) {
throw error0;
}
connection.createChannel((error1, channel) => {
if (error1) {
throw error1;
}
const queue = 'message';
channel.assertQueue(queue, { durable: false });
console.log(' [*] Waiting for message', queue);
channel.consume(
queue,
(data) => {
console.log(' [x] Received data:', data.content.toString('utf-8'));
io.emit('sendMessage', data.content.toString('utf-8'));
},
{ noAck: true }
);
});
});
}, 10000);
};
module.exports = rabbitConsumer;
index.test.js:
const amqp = require('amqplib/callback_api');
const rabbitConsumer = require('./');
describe('rabbitConsumer', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterAll(() => {
jest.useRealTimers();
});
test('should pass', () => {
const mData = {
content: 'teresa teng',
};
const mChannel = {
assertQueue: jest.fn(),
consume: jest.fn().mockImplementation((queue, callback) => {
callback(mData);
}),
};
const mConnection = {
createChannel: jest.fn().mockImplementation((callback) => {
callback(null, mChannel);
}),
};
jest.spyOn(amqp, 'connect').mockImplementation((url, callback) => {
callback(null, mConnection);
});
const mIO = {
emit: jest.fn(),
};
rabbitConsumer(mIO);
jest.advanceTimersByTime(10000);
expect(amqp.connect).toBeCalledWith('amqp://rabbitmq', expect.any(Function));
expect(mConnection.createChannel).toBeCalledWith(expect.any(Function));
expect(mChannel.assertQueue).toBeCalledWith('message', { durable: false });
expect(mChannel.consume).toBeCalledWith('message', expect.any(Function), { noAck: true });
});
});
測驗結果:
PASS examples/69715530/index.test.js (21.564 s)
rabbitConsumer
? should pass (27 ms)
console.log
[*] Waiting for message message
at examples/69715530/index.js:15:17
console.log
[x] Received data: teresa teng
at channel.consume.noAck (examples/69715530/index.js:19:21)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 87.5 | 50 | 100 | 87.5 |
index.js | 87.5 | 50 | 100 | 87.5 | 7,11
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 23.222 s
您可以創建callback模擬錯誤并將它們傳遞給模擬實作以測驗錯誤處理程式分支。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/337296.html
