doufei2355 2019-03-07 20:23
浏览 1199

如果未提供证书,则单元测试tls.LoadX509KeyPair将失败

cert, err := tls.LoadX509KeyPair(os.Getenv("CERT"), os.Getenv("KEY"))
if err != nil {
    return err
}

I want to write a unit test for a function that contains this snippet of code. However, my test environment will never have any content in os.Getenv("CERT")/os.Getenv("KEY"). This makes the code(tls.LoadX509KeyPair()) return an error, which doesn't let me test the function. How should I go about mocking/modifying this snippet?

  • 写回答

1条回答 默认 最新

  • duanchongzi9997 2019-03-07 23:07
    关注

    This works by defining vars for the certFile and keyFile and then overriding them in the test environment.

    //.. 
    var certFile = os.Getenv("CERT")
    var keyFile = os.Getenv("KEY")
    //...
    cert, err := tls.LoadX509KeyPair(certFile, keyFile)
      if err != nil {
         return err
      }
    

    Follow the below link to see how to override the vars in a test env. In Go,how to get test environment at run time?

    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题