user9528667
user9528667

Reputation:

Unit test in golang with structure

I use the VSCode generation for test file of my project,

currenlty it generate the folloing structure

tests := []struct {
        name    string
        args    args
        wantOut ZTR
    }{
        name: "test123",
        args: args{
            ztrFile: "./testdata/ztrfile.yaml",
        },
        wantOut: “ZTR.Modules",
    }

The test should cover parse of yaml and testing the properties

Here it calles to parse file

for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if gotOut := parseFile(tt.args.ztrFile); !reflect.DeepEqual(gotOut, tt.wantOut) {
                t.Errorf("parseFile() = %v, want %v", gotOut, tt.wantOut)
            }
        })

This is the struct

type Modules struct {
    Name       string
    Type       string
    cwd       string     `yaml:”cwd,omitempty"`
}

Not sure what I need to put here to make it work, I try to play with the types but Im getting errors

        {
            name: "test123",
            args: args{
                mtaFile: "./testdata/ztrfile.yaml",
            },
            wantOut: “ZTR.Modules",
        }

The errors I got is

message: 'cannot use "test123" (type string) as type struct { name string; args args; wantOut ZTR } in array or slice literal' at: '41,3' source: '' code: 'undefined'

Upvotes: 2

Views: 5902

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79556

Your tests declaration is incorrect. You need to provide a slice of structs, but you're providing just keys/values:

tests := []struct {
    name    string
    args    args
    wantOut ZTR
}{
    name: "test123",
    args: args{
        mtaFile: "./testdata/ztrfile.yaml",
    },
    wantOut: “ZTR.Modules",
}

should be:

tests := []struct {
    name    string
    args    args
    wantOut ZTR
}{
    {
        name: "test123",
        args: args{
            mtaFile: "./testdata/ztrfile.yaml",
        },
        wantOut: “ZTR.Modules",
    },
}

Upvotes: 4

Related Questions