记录一下用jest做交换机命令测试

做一个近期的记录

最近写交换机测试时遇到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)');
    })
})

测试结果:
图二

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值