mercredi 1 mars 2017

golang unit test: inner function parameters

I have simple work flow in go functions, but when come to unit test, I'm stuck at passing parameter to inner functions.

code:

package myFunc

import myPackage

func Init() (err error) {
    err = getResource(myPackage.GetPath())
    ...
}

func getResource(path string) (err error) {
    // get resource from path ...
}

test:

package myFunc

import "testing"

func TestInit(t *testing.T) {
    if err := Init(); err != nil {
        t.Fatal("test failed")
    }
}

result:

--- FAIL: TestInit (0.00s)
Failed to read path ...

If I test getResource() by directly passing in the path string, it is ok. I cannot modify the Init() function since it is from different place.

What will be the general solution to this type of scenarios? for example if getResource() calls getBooks() and its parameter comes from another source?

Thanks,

Aucun commentaire:

Enregistrer un commentaire