我是玩笑的新手,抱歉,如果這是一個微不足道的問題,但我瀏覽了官方的玩笑檔案,但我找不到問題的解決方案。
我正在 nodejs 中開發一個非常簡單的應用程式,它使用來自 websocket 的資料并通過 zeromq 將其向下游傳播到一組消費者。
代碼如下:
應用程式.js:
const initializer = require("./dependencyInitializer");
const sock = initializer.zmqSock();
const ws = initializer.wsClient();
ws.on('update', data => {
sock.send([data.topic, JSON.stringify(data)]);
});
websocket 客戶端是從 EventEmitter 擴展的第三方庫中的一個類。
我想創建一個測驗,斷言該sock.send函式在 'update' 事件的處理程式中只被呼叫一次。
這是我的方法:
app.spec.js :
const ws = require("./app");
const initializer = require("./dependencyInitializer");
jest.mock("./dependencyInitializer", () => {
return {
wsClient: jest.fn(() => {
const EventEmitter = require("events")
const emitter = new EventEmitter()
return emitter;
}),
zmqSock: jest.fn(() => {
return {
send: jest.fn()
}
})
}
});
describe('on message received from websocket',() => {
it('should pass it to zmq', () => {
const data = {result: "ok"};
expect(initializer.wsClient).toHaveBeenCalledTimes(1);
expect(initializer.zmqSock).toHaveBeenCalledTimes(1);
const _sock = initializer.zmqSock();
const _ws = initializer.wsClient();
_ws.emit("update", data);
expect(_sock.send).toHaveBeenCalledTimes(1);
});
});
測驗失敗,原因如下:
on message received from websocket ? should pass it to zmq
expect(jest.fn()).toHaveBeenCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
28 | const _ws = initializer.wsClient();
29 | _ws.emit("update", data);
> 30 | expect(_sock.send).toHaveBeenCalledTimes(1);
| ^
31 | });
32 | });
我不確定我是否走在正確的道路上,我想了解開發這樣的測驗的最佳方法是什么。
謝謝
uj5u.com熱心網友回復:
通過mocking之后jest.mock(),當檔案和檔案中的.wsClient()and.zmqSock()方法被呼叫時,里面的和物件和.app.jsapp.spec.jssockwsapp.jsapp.spec.js
{
wsClient: jest.fn(() => {
const EventEmitter = require("events")
const emitter = new EventEmitter()
return emitter;
})
}
每次呼叫時.wsClient(),它都會創建一個新物件。
發射器只能監聽來自它自己的事件emit。解決方案是在模擬工廠中創建模擬emitter和sock物件。
app.js:
const initializer = require('./dependencyInitializer');
const sock = initializer.zmqSock();
const ws = initializer.wsClient();
ws.on('update', (data) => {
sock.send([data.topic, JSON.stringify(data)]);
});
module.exports = { sock, ws };
app.test.js:
const app = require('./app');
const initializer = require('./dependencyInitializer');
jest.mock(
'./dependencyInitializer',
() => {
const EventEmitter = require('events');
const emitter = new EventEmitter();
const mSock = { send: jest.fn() };
return {
wsClient: jest.fn(() => emitter),
zmqSock: jest.fn(() => mSock),
};
},
{ virtual: true }
);
describe('on message received from websocket', () => {
it('should pass it to zmq', () => {
const data = { result: 'ok' };
expect(initializer.wsClient).toHaveBeenCalledTimes(1);
expect(initializer.zmqSock).toHaveBeenCalledTimes(1);
const _sock = initializer.zmqSock();
const _ws = initializer.wsClient();
// check if they have same reference
expect(app.sock).toBe(_sock);
expect(app.ws).toBe(_ws);
_ws.emit('update', data);
expect(_sock.send).toHaveBeenCalledTimes(1);
});
});
測驗結果:
PASS examples/70024105/app.test.js (9.279 s)
on message received from websocket
? should pass it to zmq (2 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
app.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 10.112 s
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/362643.html
上一篇:如何處理C 中的運行時錯誤?
