mardi 12 janvier 2021

Storing a type in a struct for compare

Given the following code: I'm trying to store the type of the mongo client in the struct to later be compared.

func TestGetMongoClient(t *testing.T) {
    type args struct {
        opts *Options
    }
    tests := []struct {
        name    string
        args    args
        want    *mongo.Client
        wantErr bool
    }{
        {name: "Mongo Client Creation", args: args{opts: &opts}, want: , wantErr: nil},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := GetMongoClient(tt.args.opts)
            if (err != nil) != tt.wantErr {
                t.Errorf("GetMongoClient() error = %v, wantErr %v", err, tt.wantErr)
                return
            }
            typeCompare = reflect.TypeOf(tt.want) == reflect.TypeOf(got)
            if typeCompare {
                t.Errorf("GetMongoClient() = %v, want %v", got, tt.want)
            }
        })
    }
}

Prior, I had the following code which worked well however I have to translate it into a table based testing structure.

func TestGetMongoConnection(t *testing.T) {
    client, err := GetMongoClient(&opts)
    if err != nil {
        t.Errorf("GetMongoConnect should not return an error but did %d", err)
    }
    result := reflect.TypeOf(&client) == reflect.TypeOf((**mongo.Client)(nil))
    if result == false {
        t.Errorf("Calling GetMongoConnection didn't return a client %d", err)
    }
}

How can I get the table based code to work well?

Aucun commentaire:

Enregistrer un commentaire