jeudi 1 février 2018

golang test use global variable

When i set global variable value in test i'm get panic: assignment to entry in nil map

In main file i'm have var authheader string

According golang init() in *_test.go run after non-test files

My test is:

package main

import "testing"

func init() {
    authheader = "asdaaa"
}

func Test_sendToDB(t *testing.T) {

    type args struct {
        result []byte
        path   string
    }
    tests := []struct {
        name    string
        args    args
        wantErr bool
    }{
        {"sendToDB wrong result",
            args{[]byte("Hello "), "tutututu"},
            true,
        },

        {"sendToDB nil result",
            args{nil, "tutututu"},
            true,
        },
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if err := sendToDB(tt.args.result, tt.args.path); (err != nil) != tt.wantErr {
                t.Errorf("sendToDB() error = %v, wantErr %v", err, tt.wantErr)
            }
        })
    }

}

Aucun commentaire:

Enregistrer un commentaire