下面我有以下代码和代码测试,由于某种原因,deepEqual返回false并导致测试失败。现在,从阅读有关doco的内容开始,我希望对于如此简单的事情,它会成为事实吗?任何观点将不胜感激。谢谢
// customer.go
type Customer struct {
customerID int
domains []string
names []string
}
func NewCustomer(customerID int, domains []string, names []string) *Customer{
return &Customer{
customerID: customerID,
domains: domains,
names:names,
}
}
// customer_test.go
func TestNewCustomer(t *testing.T) {
expected := &Customer{123,
[]string{},
[]string{},
}
got := NewCustomer(123, []string{}, []string{})
if got.customerID != expected.customerID {
t.Errorf("Got: %v, expected: %v", got.customerID, expected.customerID)
}
if reflect.DeepEqual(got, expected) {
t.Errorf("Got: %v, expected: %v", got, expected)
}
}
输出:
Got: &{123 [] []}, expected: &{123 [] []}
答案 0 :(得分:3)
reflect.DeepEqual()
按预期返回true
,这就是调用您的t.Errorf()
的原因。
如果不相等,则您希望测试失败。您只需取消条件即可:
if !reflect.DeepEqual(got, expected) {
t.Errorf("Got: %v, expected: %v", got, expected)
}