github.com/blend/go-sdk@v1.20220411.3/r2/r2test/doc.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  /*
     9  Package r2test provides helpers for writing tests involving calls with sdk/r2.
    10  
    11  The most common example is to add a mock response as an option to a default set of options.
    12  
    13  Lets say we have a wrapping helper client:
    14  
    15  	type APIClient struct {
    16  		RemoteURL string
    17  		...
    18  		Defaults []r2.Option
    19  	}
    20  
    21  	func (a APIClient) GetFoos(ctx context.Context) (output []Foo, err error) {
    22  		_, err = r2.New(a.RemoteURL, append(a.Defaults, r2.OptContext(ctx))...).JSON(&output)
    23  		return
    24  	}
    25  
    26  During tests we can add a default option:
    27  
    28  	mockedResponse := `[{"is":"a foo"}]`
    29  	a := APIClient{ Remote: "http://test.invalid", Defaults: []r2.Option{r2test.OptMockResponseString(mockedResponse)} }
    30  	foos, err := a.GetFoos(context.TODO())
    31  	...
    32  
    33  We will now return the mocked response instead of reaching out to the remote for the call.
    34  */
    35  package r2test // import "github.com/blend/go-sdk/r2/r2test"