英文:
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: ""},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论