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?
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?