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?

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料