mockgcsclient

package
v0.0.0-...-03d6fc4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2019 License: BSD-3-Clause Imports: 5 Imported by: 0

README

#Mocking Examples

The basic flow for making a test involving mock_gcs_client involves creating a mock, and calling On() on it to mock out the function calls.

var testBytes []byte = ...

func TestFetchFromGCS(t *testing.T) {
	testutils.SmallTest(t)
	m := mock_gcs_client.New()
	defer m.AssertExpectations(t)

	o := MyObjStruct{client:m}

	// AnythingOfType is handy when you don't really care what gets passed in, as long
	// as it matches the type specified.
	ctx := mock.AnythingOfType("*context.emptyCtx")

	m.On("GetFileContents", ctx, "my-file").Return(testBytes, nil)

	o.DoAThing()

	// Make sure we don't spam GCS with requests
	m.AssertNumberOfCalls(t, "GetFileContents", 1)
}

type MyObjStruct {
	client gs.GCSClient
}


func (o *MyObj) DoAThing() {
	...
	content, err := o.client.GetFileContents(context.Background(), path)
	...
}

If you don't mock out a call, or the parameters don't match exactly, the test will fail and give you a (hopefully) helpful message about how close the params were.

If you mock out a call that doesn't get used at all, AssertExpectations(t) will catch this and make the test fail.

mock.Anything() and mock.AnythingOfType(string) are useful for keeping the specifications too crazy.

In the above example, every call to m.GetFileContents("my-file") will return the same set of testBytes. If your test wants to change this (i.e. simulating the file goes missing), you'll need to do something like:

m.On("GetFileContents", ctx, "my-file").Return(testBytes, nil).Times(6)
m.On("GetFileContents", ctx, "my-file").Return(nil, errors.New("Not found"))

Where it will return the test bytes for the first 6 calls, and then return an error.

It is very tempting to do something like

	m.On("GetFileContents", ctx, "my-file").Return(testBytes, nil)

	o.DoAThing()

	m.On("GetFileContents", ctx, "my-file").Return(differentTestBytes, nil)

	// One might expect GetFileContents to start returning differentTestBytes, for "my-file",
	// but that is not the case.
	o.DoAThing()

But this does not work. As of this writing, unless .Times() is called, only the first m.On() is used.

##More Information

The Mock API is at https://godoc.org/github.com/stretchr/testify/mock

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockGCSClient

type MockGCSClient struct {
	mock.Mock
}

MockGCSClient is a mock of gcs.GCSClient. All the methods are mocked using testify's mocking library. See the README in this directory for some example mocks. This struct can be embedded to extend to instance-specific GCS functions. See fuzzer for an example.

func New

func New() *MockGCSClient

New returns a pointer to a newly created struct. We return the pointer because we want to make sure the methods on mock.Mock stay accessible, e.g. m.On()

func (*MockGCSClient) AllFilesInDirectory

func (m *MockGCSClient) AllFilesInDirectory(ctx context.Context, folder string, callback func(item *storage.ObjectAttrs)) error

func (*MockGCSClient) Bucket

func (m *MockGCSClient) Bucket() string

func (*MockGCSClient) DeleteFile

func (m *MockGCSClient) DeleteFile(ctx context.Context, path string) error

func (*MockGCSClient) DoesFileExist

func (m *MockGCSClient) DoesFileExist(ctx context.Context, path string) (bool, error)

func (*MockGCSClient) FileReader

func (m *MockGCSClient) FileReader(ctx context.Context, path string) (io.ReadCloser, error)

func (*MockGCSClient) FileWriter

func (m *MockGCSClient) FileWriter(ctx context.Context, path string, opts gcs.FileWriteOptions) io.WriteCloser

func (*MockGCSClient) GetFileContents

func (m *MockGCSClient) GetFileContents(ctx context.Context, path string) ([]byte, error)

func (*MockGCSClient) SetFileContents

func (m *MockGCSClient) SetFileContents(ctx context.Context, path string, opts gcs.FileWriteOptions, contents []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL