Re: [go-nuts] How to write unit test for init()?

2018-11-26 Thread Mhd Shulhan
On Tue, 27 Nov 2018, 11:08 Vast Peng 
>
>   when importing *component*, Golang will call *init() *automatically.
>

One way to do it is by moving the init into another function and call it
from init.  That way you can create test for the function.

>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] How to write unit test for init()?

2018-11-26 Thread Vast Peng
The code below:

package component

import "os"

type BaseicConfig struct{
}

func init() {
 configDir := os.Getenv(constant.ProjectRootEnv)
 if configDir == "" {
 // run here.
 log.Panic(constant.InvalidEnvVariables)
 }


 configPath := filepath.Join(configDir, constant.ConfigName)
 if _, err := toml.DecodeFile(configPath, ); err != nil {
 log.Panic(err.Error())
 }
}

I want to test it.

Problem:

  when importing *component*, Golang will call *init() *automatically.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.