google-cloud-go-testing – github.com/googleapis/google-cloud-go-testing Index | Examples | Files | Directories

package googlecloudgotesting

import "github.com/googleapis/google-cloud-go-testing"

These packages contain code that can help you test against the GCP Client Libraries for Go (https://github.com/GoogleCloudPlatform/google-cloud-go).

We do not recommend using mocks for most testing. Please read https://testing.googleblog.com/2013/05/testing-on-toilet-dont-overuse-mocks.html.

Note: These packages are in alpha. Some backwards-incompatible changes may occur.

Embedding Interfaces

All interfaces in this package include an embedToIncludeNewMethods method. This is intentionally unexported so that any implementor of the interface must embed the interface in their implementation. Embedding the interface in an implementation has the effect that any future methods added to the interface will not cause compile-time errors (the implementation does not implement the newly-added method), since embedded interfaces provide a default method for unimplemented methods.

See Example (RecordBuckets) for an example of how to implement interfaces (including embedding the interface).

Example (RecordBuckets)

Code:play 

package main

import (
	"context"
	"fmt"

	"cloud.google.com/go/storage"
	"github.com/googleapis/google-cloud-go-testing/storage/stiface"
)

type RecordingClient struct {
	stiface.Client
	bucketCalls int
}

func (rc *RecordingClient) Bucket(name string) stiface.BucketHandle {
	rc.bucketCalls++
	return rc.Client.Bucket(name)
}

// We do not need to implement methods that we don't want to record - by default
// the embedded type will be used.

func main() {
	// This example demonstrates building a simple mock that counts the number
	// of Bucket calls before calling the real client and returning its output.

	ctx := context.Background()
	c, err := storage.NewClient(ctx)
	if err != nil {
		// TODO: Handle error.
	}
	client := stiface.AdaptClient(c)
	recordingClient := RecordingClient{client, 0}

	recordingClient.Bucket("my-bucket-1")
	recordingClient.Bucket("my-bucket-2")
	recordingClient.Bucket("my-bucket-3")

	fmt.Println(recordingClient.bucketCalls)
}

Output:

3

Index

Examples

Source Files

doc.go

Directories

PathSynopsis
bigquery
bigquery/bqifacePackage bqiface provides a set of interfaces for the types in cloud.google.com/go/bigquery.
datastore
datastore/dsifacePackage dsiface provides a set of interfaces for the types in cloud.google.com/go/datastore.
pubsub
pubsub/psifacePackage psiface provides a set of interfaces for the types in cloud.google.com/go/pubsub.
storage
storage/stifacePackage stiface provides a set of interfaces for the types in cloud.google.com/go/storage.
Version
v0.0.0-20210719221736-1c9a4c676720 (latest)
Published
Jul 19, 2021
Platform
linux/amd64
Last checked
3 weeks ago

Tools for package owners.