How to check if a specific function is called in Golang

I am currently trying my best to write TDD in Go. However, I adhered to the following.

Write test:

func TestFeatureStart(t *testing.T){

}

Implementation for testing:

func (f *Feature) Start() error {
  cmd := exec.Command(f.Cmd)
  cmd.Start()
}

How to check this simple bit. I decided that I only want to check that the exec library works correctly. (How could I use Java with Mockito). Can someone help me write this test because I'm not quite sure what the current available answers on the Internet mean. They suggest using interfaces.

Currently, Feature-struct contains only the Cmd line.

+4
source share
1 answer

, . :

var cmdStart = (*exec.Cmd).Start
func (f *Feature) Start() error {
    cmd := exec.Command(f.Cmd)
    return cmdStart(cmd)
}

:

called := false
cmdStart = func(*exec.Cmd) error { called = true; return nil }
f.Start()
if !called {
    t.Errorf("command didn't start")
}

: Andrew Gerrand .

+5

Source: https://habr.com/ru/post/1606165/


All Articles