将测试数据传递给嵌套结构体。

huangapple go评论85阅读模式
英文:

Pass test data to nested struct

问题

我正在努力理解Go语言中的测试,并遇到了一些困难。

我有以下代码:

package main

import (
	"fmt"
	"testing"
)

type ConformanceChange struct {
	Val   string
	Proxy struct {
		Address string
		Port    string
	}
}

func Item(conformance ConformanceChange) string {
	service := conformance.Proxy.Address
	services := map[string]string{
		"Word": "Now",
	}
	service = services[service]
	fmt.Println("Service: ", service)
	return service
}

func Test_Item(t *testing.T) {
	type args struct {
		conformance ConformanceChange
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		// TODO: Add test cases.
		{name: "empty", args: args{
			conformance: ConformanceChange{},
		}, want: ""},
		{name: "value", args: args{
			conformance: ConformanceChange{Val: "test", Proxy: struct {
				Address string
				Port    string
			}{Address: "d", Port: "d"}},
		}, want: ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Item(tt.args.conformance); got != tt.want {
				t.Errorf("getService() = %v, want %v", got, tt.want)
			}
		})
	}
}

我想在运行测试时向ConformanceChange.Proxy.Address传递数据。我可以向ConformanceChange.Val传递数据并且可以正常工作。

有人可以告诉我正确的语法吗?

更新
Bless提供的解决方案有效。

接下来,如果我有以下代码:

package main

import (
	"fmt"
	"testing"
)

type ConformanceChange struct {
	Val   string
	Proxy struct {
		Address string `json:address`
		Port    string `json:port`
	}
}

func Item(conformance ConformanceChange) string {
	service := conformance.Proxy.Address
	services := map[string]string{
		"Word": "Now",
	}
	service = services[service]
	fmt.Println("Service: ", service)
	return service
}

func Test_Item(t *testing.T) {
	type args struct {
		conformance ConformanceChange
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		// TODO: Add test cases.
		{name: "empty", args: args{
			conformance: ConformanceChange{},
		}, want: ""},
		{name: "value", args: args{
			conformance: ConformanceChange{Val: "test", Proxy: struct {
				Port    string `json:port`
			}(struct {
				Port    string
			}{Port: "d"})},
		}, want: ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Item(tt.args.conformance); got != tt.want {
				t.Errorf("getService() = %v, want %v", got, tt.want)
			}
		})
	}
}

我不想在我的测试中指定所有字段(因为可能会变得混乱),我只能指定其中一个字段吗?在上面的代码中,它失败并显示以下错误:
Cannot use 'struct { Port string json:port }(struct { Port string }{Port: "d"})' (type struct {...}) as the type struct {...}

英文:

I am trying to get to grips with testing in Go and I have hit a bit of a stumbling block.

I have the following code:

package main

import (
	"fmt"
	"testing"
)

type ConformanceChange struct {
	Val   string
	Proxy struct {
		Address string
		Port    string
	}
}

func Item(conformance ConformanceChange) string {
	service := conformance.Proxy.Address
	services := map[string]string{
		"Word": "Now",
	}
	service = services[service]
	fmt.Println("Service: ", service)
	return service
}

func Test_Item(t *testing.T) {
	type args struct {
		conformance ConformanceChange
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		// TODO: Add test cases.
		{name: "empty", args: args{
			conformance: ConformanceChange{},
		}, want: ""},
		{name: "value", args: args{
			conformance: ConformanceChange{Val: "test", Proxy: {Address: "d", Port: "d"}},
		}, want: ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Item(tt.args.conformance); got != tt.want {
				t.Errorf("getService() = %v, want %v", got, tt.want)
			}
		})
	}
}

I would like to be able to pass data to ConformanceChange.Proxy.Address when running my tests. I can pass data to ConformanceChange.Val and it works.

Can someone please let me know the correct syntax for this?

Update
The solution provided by Bless worked.

Following on from this, if I had the following code:

package main

import (
	"fmt"
	"testing"
)

type ConformanceChange struct {
	Val   string
	Proxy struct {
		Address string `json:address`
		Port    string `json:port`
	}
}

func Item(conformance ConformanceChange) string {
	service := conformance.Proxy.Address
	services := map[string]string{
		"Word": "Now",
	}
	service = services[service]
	fmt.Println("Service: ", service)
	return service
}

func Test_Item(t *testing.T) {
	type args struct {
		conformance ConformanceChange
	}
	tests := []struct {
		name string
		args args
		want string
	}{
		// TODO: Add test cases.
		{name: "empty", args: args{
			conformance: ConformanceChange{},
		}, want: ""},
		{name: "value", args: args{
			conformance: ConformanceChange{Val: "test", Proxy: struct {
				Port    string `json:port`
			}(struct {
				Port    string
			}{Port: "d"})},
		}, want: ""},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Item(tt.args.conformance); got != tt.want {
				t.Errorf("getService() = %v, want %v", got, tt.want)
			}
		})
	}
}

I don't want to specify all the fields in my test (as it could get messy), can I just specify the one? In the above code it fails with:
Cannot use 'struct { Port string json:port }(struct { Port string }{Port: "d"})' (type struct {...}) as the type struct {...}

答案1

得分: 2

创建结构值时,应指定结构。

{name: "value", args: args{
    conformance: ConformanceChange{Val: "test", Proxy: struct{Address string; Port string}{Address: "d", Port: "d"}},
}, want: ""},

为了避免重复,您可以定义一个Proxy结构

type Proxy struct {
    Address string
    Port    string
}

type ConformanceChange struct {
    Val   string
    Proxy Proxy
}

然后在初始化结构值时引用它

{name: "value", args: args{
    conformance: ConformanceChange{Val: "test", Proxy: Proxy{Address: "d", Port: "d"}},
}, want: ""},
英文:

When creating a struct value, the struct should be specified.

{name: "value", args: args{
    conformance: ConformanceChange{Val: "test", Proxy: struct{Address string; Port string}{Address: "d", Port: "d"}},
}, want: ""},

To avoid repetition, you could define a Proxy struct

type Proxy struct {
    Address string
    Port    string
}

type ConformanceChange struct {
    Val   string
    Proxy Proxy
}

and then refer to it when initialising the struct value

{name: "value", args: args{
    conformance: ConformanceChange{Val: "test", Proxy: Proxy{Address: "d", Port: "d"}},
}, want: ""},

huangapple
  • 本文由 发表于 2021年8月2日 06:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/68614970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定