在GoLang中保持状态跨测试

时间:2018-10-14 06:39:21

标签: go testing

我希望实例化一个对象,并通过Go Test中的多个函数调用更新该对象的状态。

我的主要逻辑如下:

type User struct {
    Username string
    Persist4 map[string]string
}


func Init(username string, password string) (userdataptr *User, err error) {
    mapping2 := make(map[string]string)
    var userdata User
    userdata.Username = username
    userdata.Persist5 = mapping2
    // Marshak and store this structure 
    return &userdata, err
}


func Get(uname string, password string) (user *User, err error) {
    // Retrieve and unmarshal user into tmp
    return &temp, nil
}

func (userdata *User) StoreFile(filename string, data []byte) {
    userdata.Persist4['test'] = 'fails' // This is not available in the next call 

}

测试逻辑如下:

func TestInit(t *testing.T) {
    u, _ := Init("a", "f")

}

func TestStorage(t *testing.T) {
    u, err := GetUser("a", "f")

    u.StoreFile("file1", v)

}

func test(t *testing.T) {
    u, err := Get("a", "f")
    // u no longer has attribute persist4['test'] = 'fails'
}

0 个答案:

没有答案