英文:
how to handle New Server func in unit testing
问题
对于我来说,单元测试有一定的工作量,所以我使用gotests来生成样板测试代码。
server.go
func NewServer(cfg *Config, l net.Listener, driver Driver, db store.Store) *Server {
s := &Server{
cfg: cfg,
listener: l,
leader: "",
driver: driver,
db: db,
}
s.server = &http.Server{
Handler: s.createMux(),
}
return s
}
使用gotests生成server_test.go:
func TestNewServer(t *testing.T) {
fakeCfg := &Config{
Listen: "hello",
LogLevel: "debug",
}
type args struct {
cfg *Config
l net.Listener
leader string
driver Driver
db store.Store
}
tests := []struct {
name string
args args
want *Server
}{
{
name: "test",
args: args{
cfg: fakeCfg,
},
want: &Server{
cfg: fakeCfg,
server: &http.Server{
Handler: nil,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewServer(tt.args.cfg, tt.args.l, tt.args.driver, tt.args.db); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewServer() = %v, want %v", got, tt.want)
}
})
}
}
单元测试结果:
$ go test -v -run TestNewServer
=== RUN TestNewServer
=== RUN TestNewServer/test
--- FAIL: TestNewServer (0.00s)
--- FAIL: TestNewServer/test (0.00s)
server_test.go:47: NewServer() = &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6840 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}, want &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6790 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}
FAIL
exit status 1
FAIL github.com/Dataman-Cloud/swan/api 0.017s
因为初始的server结构不是一步完成的,所以我无法在&Server{}
部分正确获取server属性。
有人可以帮我一下,给我一个提示吗?如何在这种情况下编写测试?
英文:
for me , unit testing has workload. so i use gotests to generate Boilerplate testing code case.
server.go
func NewServer(cfg *Config, l net.Listener, driver Driver, db store.Store) *Server {
s := &Server{
cfg: cfg,
listener: l,
leader: "",
driver: driver,
db: db,
}
s.server = &http.Server{
Handler: s.createMux(),
}
return s
}
gotests generate server_test.go:
func TestNewServer(t *testing.T) {
fakeCfg := &Config{
Listen: "hello",
LogLevel: "debug",
}
type args struct {
cfg *Config
l net.Listener
leader string
driver Driver
db store.Store
}
tests := []struct {
name string
args args
want *Server
}{
{
name: "test",
args: args{
cfg: fakeCfg,
},
want: &Server{
cfg: fakeCfg,
server: &http.Server{
Handler: nil,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewServer(tt.args.cfg, tt.args.l, tt.args.driver, tt.args.db); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewServer() = %v, want %v", got, tt.want)
}
})
}
}
unit test result:
$ go test -v -run TestNewServer
=== RUN TestNewServer
=== RUN TestNewServer/test
--- FAIL: TestNewServer (0.00s)
--- FAIL: TestNewServer/test (0.00s)
server_test.go:47: NewServer() = &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6840 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}, want &{cfg:0xc4201f5580 listener:<nil> leader: server:0xc4201d6790 driver:<nil> db:<nil> Mutex:{state:0 sema:0}}
FAIL
exit status 1
FAIL github.com/Dataman-Cloud/swan/api 0.017s
because the initial server struct is not one step. i can't correct get server attribute in &Server{} section.
anyone can do me a favor, give me a hint?howto write test on this situation?
答案1
得分: 2
你正在比较指针而不是值。
你应该将测试改为:
!reflect.DeepEqual(*got, *tt.want)
然后你就在比较结构体的内容。
英文:
You are comparing the pointers and not the values.
You should change your test to:
!reflect.DeepEqual(*got, *tt.want)
Then you are comparing the content of the structs.
答案2
得分: 0
最后,我进行了一个巧妙的测试,我手动组合了对象的每个元素。它运行得非常好。
英文:
finally i do a trick test, i manually compose every element of the object. it works like a charm.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论