测试设置:Minitest+Capybara-poltergeist + phantomjs。
当我开始在无头模式下运行测试时,推送器会将消息记录到控制台。有时这些消息太多,以至于我错过了测试写入的日志。我尝试通过执行以下操作来忽略这些消息,但这没有帮助。
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, stdout: nil, phantomjs_logger: nil)
end
推送消息示例 -
Pusher:状态更改:正在连接 -> 已连接
Pusher:状态更改:初始化 -> 连接
Pusher:连接:{"transport":"ws","url":"wss://ws.pusherapp.com:443/{删除了此处的文本}2?protocol=7&client=js&version=3.0.0&flash=false “}
Pusher:状态更改:正在连接 -> 已连接
Pusher :发送事件:{"event":"pusher:subscribe","data":{"auth":"{删除此处的文本}","channel":"private-facility-{删除此处的文本} }}
Pusher:事件记录:{"event":"pusher_internal:subscription_succeeded","data":{},"channel":"private-facility-{删除了此处的文本}}
Pusher:Pusher 的私有-{删除此处的文本}没有回调:subscription_succeeded
Pusher:状态更改:初始化 -> 连接
有关如何停止登录控制台的任何输入?
Pusher JavaScript 库仅在已为
Pusher.log
属性分配了执行此操作的函数时才记录到控制台:
Pusher.log = function(msg) {
console.log(msg);
};
为了确保测试中不会发生日志记录,您应该确保没有编写与上面示例类似的代码。为了确保您可以在运行测试之前简单地
null
Pusher.log
属性。
有关更多信息,请参阅Pusher JavaScript 日志记录上的文档。
添加这些 Pusher.logToConsole = false;例如:“useEffect(() => { Pusher.logToConsole = false;
const pusher = new Pusher(import.meta.env.VITE_APP_PUSHERSOCKETS_ID, {
cluster: import.meta.env.VITE_APP_PUSHER_CLUSTER,
});
const channel = pusher.subscribe("sample-channel");
channel.bind("sample-event", function (data) {
setEvent({ ...event, data: data, status: true });
});
if (event.status) {
setTimeout(() => {
setEvent({ ...event, status: false });
}, 1000);
}
// Cleanup function to unsubscribe when the component unmounts
return () => {
channel.unbind_all();
channel.unsubscribe();
};
}, []);"