mardi 6 septembre 2016

Mocking a single function in a regular struct in Golang

I have a struct with a couple of functions, which I want to test -

type ExampleStruct struct {
    name string
}

func (es *ExampleStruct) TestFunc1() {
    val := es.TestFunc2()
    // Do something with val, which we want to test
}

func (es *ExampleStruct) TestFunc2() int {
    fmt.Println("Hello!")
    return 10
}

Now, my tests look something like this -

  1. Test that TestFunc2 works properly
  2. Test that TestFunc1 works properly.

Thing is, TestFunc1 depends on TestFunc2.

How do I stub TestFunc2, so that it returns a mock value, and gives me the regular Mock capabilities that let me assert that the function was called?

I'm using http://ift.tt/XuAJS0 , but for the sake of it I'll move to another library if there's one that enables these things.

Thanks

Aucun commentaire:

Enregistrer un commentaire