Với những trường hợp hai kiểm tra:kiểm tra cho bình đẳng sâu sắc với marshaling json theo golang
func TestEqualWhat(t *testing.T) {
testMarshalUnmarshal(t, map[string]interface{}{"a":"b"})
testMarshalUnmarshal(t, map[string]interface{}{"a":5})
}
Trường hợp helper testMarshalUnmarshal chỉ marshals để JSON và sao lưu ra:
func testMarshalUnmarshal(t *testing.T, in map[string]interface{}) {
//marshal the object to a string
jsb, err := json.Marshal(in);
if err != nil {
log.Printf("Unable to marshal msg")
t.FailNow()
}
//unmarshal to a map
res := make(map[string]interface{})
if err := json.Unmarshal(jsb, &res); err != nil { t.FailNow() }
if !reflect.DeepEqual(in, res) {
log.Printf("\nExpected %#v\nbut got %#v", in, res)
t.FailNow()
}
}
Tại sao trường hợp thử nghiệm đầu tiên vượt qua và lần thứ hai thất bại? Kết quả của thử nghiệm là:
Expected map[string]interface {}{"a":5}
but got map[string]interface {}{"a":5}
--- FAIL: TestEqualWhat (0.00 seconds)
Here is similar code on the go playground để bạn có thể dễ dàng hack nó.