gomock优化diff展示
使用gomock时可以调用EXPECT()为你的mock对象设置各种期望和返回值,用于验证期望值和运行时的值是否一致。通常情况下可以通过gomock来清晰的看出期望值和真实值的区别,但是当数据结构为指针切片时就只能看到下面这种提示,不能清晰的看出二者的区别是什么。
Got: [0xc000060610] ([]*demo.Item) Want: is equal to [0xc0000605b0] ([]*demo.Item)
可以通过设置 gomock Modifying Failure Messages的能力来优化这一点,举例如下。
// 优化前错误日志如下, 此时只能看到数据有 diff, 但不知道是什么 //--- FAIL: TestSaveToFile (1.00s) // demo/demo.go:12: Unexpected call to *demo.MockSaver.Save([[0xc000060610]]) at demo/demo.go:12 because: // expected call at demo/demo_test.go:14 doesn't match the argument at index 0. // Got: [0xc000060610] ([]*demo.Item) // Want: is equal to [0xc0000605b0] ([]*demo.Item) // demo/controller.go:269: missing call(s) to *demo.MockSaver.Save(is equal to [0xc0000605b0] ([]*demo.Item)) demo/demo_test.go:14 // demo/controller.go:269: aborting test due to missing call(s) func TestSaveToFile(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockSaver := NewMockSaver(ctrl) mockSaver.EXPECT().Save([]*Item{{Name: "bar"}}) Save(mockSaver, []*Item{{Name: "foo"}}) } // 优化后错误日志如下,此时不仅能看到diff,也能看到差异是什么 //--- FAIL: TestSaveToFileV2 (0.00s) // demo/demo.go:12: Unexpected call to *demo.MockSaver.Save([[0xc00009e5f0]]) at demo/demo.go:12 because: // expected call at demo/demo_test.go:23 doesn't match the argument at index 0. // Got: [{"Name":"foo"}] // Want: [{"Name":"bar"}] // demo/controller.go:269: missing call(s) to *demo.MockSaver.Save([{"Name":"bar"}]) demo/demo_test.go:23 // demo/controller.go:269: aborting test due to missing call(s) func TestSaveToFileV2(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockSaver := NewMockSaver(ctrl) mockSaver.EXPECT().Save(eq([]*Item{{Name: "bar"}})) // 注意这里调用了 eq 方法 Save(mockSaver, []*Item{{Name: "foo"}}) } func eq(want interface{}) gomock.Matcher { return gomock.GotFormatterAdapter( gomock.GotFormatterFunc( func(got interface{}) string { b, _ := json.Marshal(got) return string(b) }, ), gomock.WantFormatter( gomock.StringerFunc(func() string { b, _ := json.Marshal(want) return string(b) }), gomock.Eq(want), ), ) }