做一个近期的记录
最近写交换机测试时遇到Received为“{}”,如下图
多次测试后发现原来FirstTest.run()返回的是一个promise。因为原文件用了async,测试文件直接调用了函数。因此只需要将测试文件也加上异步即可。
下面附上源文件代码:
export default class FirstTest {
static telnet = require('telnet-client');
static async run() : Promise<any> {
let connection = new this.telnet()
let params = {
host: '', // 交换机ip
port: 23,
timeout: 1000
}
try{
await connection.connect(params)
}catch(error){
console.log(error);
}
await connection.exec('con t');
let information = await connection.exec('vlan 10');
// console.log("信息1:", information1)
// console.log("信息:", information)
await connection.end();
return information;
}
}
测试文件代码:
import FirstTest from "../src/firsttest";
describe("test vlan", () => {
test("run", async () => {
expect(await FirstTest.run()).toBe('Switch(config-vlan)');
})
})
测试结果: