我目前使用Twilio Node Helper Library来做各种API调用,无论是创建助手/服务,列出它们,删除它们以及其他各种事情,当涉及到上传任务,示例,字段以在Twilio Autopilot上创建聊天机器人时。
这些函数中的一个示例包括:
async function createAssistant(name, client){
var assistantUid = ""
await client.autopilot.assistants
.create({
friendlyName: name,
uniqueName: name
})
.then(assistant => assistantUid = assistant.sid);
return assistantUid
}
async function getAccount(client){
var valid = true
try {
await client.api.accounts.list()
} catch (e) {
valid = false
}
return valid
}
async function connectToTwilio(twilioAccountSid, twilioAuthToken) {
try{
var client = twilio(twilioAccountSid, twilioAuthToken);
} catch (e){
throw new TwilioRequestError(e)
}
var valid = await getAccount(client)
if(valid && client._httpClient.lastResponse.statusCode === 200){
return client
} else{
throw new Error("Invalid Twilio Credentials")
}
}其中client是从require("twilio")(twilioAccountSid,twilioAuthToken)返回的客户端对象。
我想知道模拟这个API的最好方法是什么,让我模拟创建助手,返回他们的uniqueNames等等。
我想知道我可能只是定义了一些类,比如
class TwilioTestClient{
constructor(sid, token){
this.sid = sid
this.token = token
this.assistants = TwilioAssistant()
this.services = TwilioServices()
}
}其中TwilioAssitant和TwilioServices是额外的类。
任何帮助都将不胜感激!
发布于 2021-04-30 22:42:33
我为嘲笑Twilio挣扎了很长一段时间。实际上,我以前设计过我的应用程序,这样我就可以模拟Twilio Node Helper周围的包装器,以避免模拟实际的库。但最近对架构的更改意味着这不再是一种选择。今天早上,我终于得到了Twilio Node Helper库工作的模拟。我不熟悉您正在使用的Twilio库的各个部分,但我希望这里的示例能对您有所帮助。
我们有一个函数来检查一个手机号码是否是移动的,称之为isMobile.js。
const Twilio = require("twilio");
const isMobile = async (num) => {
const TwilioClient = new Twilio(process.env.TWILIO_SID, process.env.TWILIO_AUTH_TOKEN);
try {
const twilioResponse = await TwilioClient.lookups.v1
.phoneNumbers(num)
.fetch({ type: "carrier", mobile_country_code: "carrier" });
const { carrier: { type } = {} } = twilioResponse;
return type === "mobile";
} catch (e) {
return false;
}
};
module.exports = isMobile;然后在__mocks__/twilio.js中为Twilio构建一个模拟
const mockLookupClient = {
v1: { phoneNumbers: () => ({ fetch: jest.fn(() => {}) }) }
};
module.exports = class Twilio {
constructor(sid, token) {
this.lookups = mockLookupClient;
}
};在测试文件isMobile.test.js中
jest.mock("twilio");
const Twilio = require("twilio");
const isMobile = require("./isMobile");
const mockFetch = jest.fn();
const mockPhoneNumbers = jest.fn(() => ({
fetch: mockFetch
}));
describe("isMobile", () => {
const TwilioClient = new Twilio(process.env.TWILIO_SID, process.env.TWILIO_AUTH_TOKEN);
const lookupClient = TwilioClient.lookups.v1;
lookupClient.phoneNumbers = mockPhoneNumbers;
beforeEach(() => {
mockFetch.mockReset();
});
test("is a function", () => {
expect(typeof isMobile).toBe("function");
});
test("returns true for valid mobile number", async () => {
const validMobile = "+14037007492";
mockFetch.mockReturnValueOnce({
carrier: { type: "mobile", mobile_country_code: 302 }, // eslint-disable-line camelcase
phoneNumber: validMobile
});
expect(await isMobile(validMobile)).toBe(true);
});
test("returns false for non-mobile number", async () => {
const invalidMobile = "+14035470770";
mockFetch.mockReturnValueOnce({
carrier: { type: "not-mobile", mobile_country_code: null }, // eslint-disable-line camelcase
phoneNumber: invalidMobile
});
expect(await isMobile(invalidMobile)).toBe(false);
});
});https://stackoverflow.com/questions/66556923
复制相似问题