简体   繁体   中英

Is there a good way to update structs passed to sub-tests in Golang

I am trying to write a table-driven test to test, say, if two orders passed in to a function are the same,

where Order could something like

type Order struct {
    OrderId string
    OrderType string
}

Now right now, my test looks like:

func TestCheckIfSameOrder(t *testing.T) {
    currOrder := Order{
         OrderId: "1",
         OrderType: "SALE"
    }
    oldOrder := Order{
         OrderId: "1",
         OrderType: "SALE"
    }
    tests := []struct {
        name string
        curr Order
        old  Order
        want bool
    }{
        {
            name: "Same",
            curr: currOrder,
            old:  oldOrder,
            want: true,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := checkIfSameOrder(tt.curr, tt.old)
            if got != tt.want {
                t.Errorf("want %v: got %v", tt.want, got)
            }
        })
    }

}

func checkIfSameOrder(currOrder Order, oldOrder Order) bool {
    if currOrder.OrderId != oldOrder.OrderId {
        return false
    }
    if currOrder.OrderType != oldOrder.OrderType {
        return false
    }
    return true
}

What I want to do is to add a second test, where I change the OrderId on the currOrder or something and so that my tests slice looks like

tests := []struct {
        name string
        curr Order
        old  Order
        want bool
    }{
        {
            name: "Same",
            curr: currOrder,
            old:  oldOrder,
            want: true,
        },
        {
            name: "Different OrderId",
            curr: currOrder, <-- where this is the original currOrder with changed OrderId
            old:  oldOrder,
            want: false,
        },
    }

Seems to me like I can't use a simple []struct and use something where I pass in a function, but I can't seem to find how to do that anywhere. I'd appreciate if anybody could point me in the right direction. Thanks!

If only the OrderId is different from each test, you can just pass the order id and construct oldOrder and currOrder based on that inside loop.
Sharing global variable that you mutate throughout processes has caused confusion all the time. Better initialize new variable for each test.

func TestCheckIfSameOrder(t *testing.T) {

    tests := []struct {
        name string
        currId string
        oldId  string
        want bool
    }{
        {
            name: "Same",
            currId: "1",
            oldId:  "1",
            want: true,
        },
        {
            name: "Different",
            currId: "1",
            oldId:  "2",
            want: true,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            curr := Order{
                OrderId: tt.currId,
                OrderType: "SALE"
            }
            old := Order{
                OrderId: tt.oldId,
                OrderType: "SALE"
            }

            got := checkIfSameOrder(curr, old)
            if got != tt.want {
                t.Errorf("want %v: got %v", tt.want, got)
            }
        })
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM