Lay*_*ues 6 unit-testing twilio jestjs
我一直在使用 Jest 来对 Node 进行单元测试。
我习惯于模拟模块/函数的第一级,但是在模拟 Twilio 的挑战中,我没有那么幸运。
我正在使用 twilio 方法:client.messages.create,所以这里我有来自构造函数的 twilio 客户端require('twilio')(account sid, token),第一层来自对象/方法(?)messages,最后是第三层create,这就是我试图模拟的最后一个人。
我正在尝试这样的事情:
jest.mock('twilio', () => {
const mKnex = {
messages: jest.fn(),
};
return jest.fn(mKnex);
});
Run Code Online (Sandbox Code Playgroud)
但是,我无法模拟client我得到的解析值client.message.create is not a function。如果我尝试上面的模拟加上这个,我会发现无法从(消息)client.messages.create.mockReturnValueOnce({sid: "FOO", status: "foo");读取属性。createundefined
有什么提示、帖子、文档可以给我带来一些运气吗?谢谢
Col*_*lin -2
我找到了解决方案。它仍在调用端点,但对于每个 twilio 帐户,您都会获得一个测试 SID 和令牌,我使用了这个,因此在测试时它不会发送短信:
if (process.env.NODE_ENV !== 'test') {
client = require('twilio')(accountSid, authToken)
listener = app.listen(3010, function(){
console.log('Ready on port %d', listener.address().port)
})
}else{
client = require('twilio')(testSid, testToken)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4665 次 |
| 最近记录: |