Documentation ¶
Overview ¶
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) ¶
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
Directories ¶
Path | Synopsis |
---|---|
bigquery
|
|
bqiface
Package bqiface provides a set of interfaces for the types in cloud.google.com/go/bigquery.
|
Package bqiface provides a set of interfaces for the types in cloud.google.com/go/bigquery. |
datastore
|
|
dsiface
Package dsiface provides a set of interfaces for the types in cloud.google.com/go/datastore.
|
Package dsiface provides a set of interfaces for the types in cloud.google.com/go/datastore. |
pubsub
|
|
psiface
Package psiface provides a set of interfaces for the types in cloud.google.com/go/pubsub.
|
Package psiface provides a set of interfaces for the types in cloud.google.com/go/pubsub. |
storage
|
|
stiface
Package stiface provides a set of interfaces for the types in cloud.google.com/go/storage.
|
Package stiface provides a set of interfaces for the types in cloud.google.com/go/storage. |