Git Product home page Git Product logo

Comments (2)

nash567 avatar nash567 commented on June 25, 2024 1

@sazzer, one workaround you can do is to create separate gock request instances for each test case and flush it off when the test case is executed.

eg.

type institution struct { ID string ShortName string }

type institutionResponse struct { Institutions []institution }

func NewTestGock() (*gock.Request, string) { baseURL := "https://localhost:63564" return gock.New(baseURL), baseURL }

`
func TestClient_Get(t *testing.T) {
t.Parallel()
expectedResp := institutionResponse{
Institutions: []institution{
{ID: "1033", ShortName: "CommBank"}},
}

type args struct {
	ctx  context.Context
	path string
}
tests := []struct {
	name    string
	args    args
	want    []byte
	wantErr bool
}{
	{
		name: "Success",
		args: args{
			ctx:  metadata.NewOutgoingContext(context.Background(), metadata.Pairs(interceptors.AuthMetadataKey, "token-value")),
			path: "v1/Finances/institutions",
		},
		want: func() []byte {
			data, _ := json.Marshal(expectedResp)
			return data
		}(),
		wantErr: false,
	},
	{
		name: "Unauthorized",
		args: args{
			ctx:  context.Background(),
			path: "v1/Finances/institutions",
		},
		want:    nil,
		wantErr: true,
	},
}
for _, tC := range tests {
	tt := tC
	t.Run(tt.name, func(t *testing.T) {
		t.Parallel()
		req, url := NewTestGock()
		defer gock.Remove(req.Mock)
		req.Get("v1/Finances/institutions").
			HeaderPresent("Authorization").
			Reply(200).
			JSON(expectedResp)
		c := coreapi.NewClient(&config.CoreAPI{
			BaseURL: url,
		})
		got, err := c.Get(tt.args.ctx, tt.args.path)
		if (err != nil) != tt.wantErr {
			t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
			return
		}
		if !reflect.DeepEqual(bytes.TrimRight(got, "\n"), bytes.TrimRight(tt.want, "\n")) {
			t.Errorf("Get() got = %v, want %v", got, tt.want)
		}
	})
}

}`

from gock.

steffakasid avatar steffakasid commented on June 25, 2024

Doesn't work for me...

I just disabled the t.Parrallel() for the few cases I had...

from gock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.