...
Code Block | ||||
---|---|---|---|---|
| ||||
import ( "testing" ) func TestGetDirPath(t *testing.T) { imageClient := &ImageClient{"test_image", "test_meta"} actual, dir, err := imageClient.GetDirPath("test") var expected = "/home/enyi/images/test_image/test" if actual != expected { t.Errorf("Path was incorrect, got: %s, want %s.", actual, expected) } } |
The TestGetDirPath (Test test functions begin with 'Test') function uses the testing package to print a message before failing. You can explore the testing package here https://golang.org/pkg/testing/.
...
Next, we'll mock out the user package with Testify and rewrite our test.
If you're using go modules for dependency management, you don't have to install Testify. Otherwise, run:
go get https://github.com/stretchr/testify
To mock a third party application we use an interface. This allows us to create a method, GetUser, that uses the package that we'd like to mock.
So, ultimately, we'll have two definitions of our interface: actual definition and mock definition.
The interface:
Code Block | ||||
---|---|---|---|---|
| ||||
type ImageManager interface { GetCurrentUser (*user.User) } |
References:
...