所以我正在使用嘲弄和作证在golang中进行单元测试测试代码如下:const bufSize = 1024 * 1024var lis *bufconn.Listenervar mockAccountService = &mocks.AccountService{}func init() { lis = bufconn.Listen(bufSize) s := grpc.NewServer() RegisterAccountManagementHandlerServer(s, &server{mockAccountService}) go func() { if err := s.Serve(lis); err != nil { log.Fatalf("Server exited with error: %v", err) } }()}func bufDialer(context.Context, string) (net.Conn, error) { return lis.Dial()}func TestSayHello(t *testing.T) { var a uint64 = 1 ctx := context.Background() conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure()) if err != nil { t.Fatalf("Failed to dial bufnet: %v", err) } defer conn.Close() client := NewAccountManagementHandlerClient(conn) mockAccountService.On("GetSavingAccount", context.Background(), a, a, "Hello", 1).Return(&models.SavingAccount{ CustomerID: 1, ID: 1, CardNo: "Hello", SavingProductID: 1, Balance: 0, Status: 1, }) resp, err := client.GetSavingAccount(ctx, &GetSavingAccountDataRequest{ Id: 1, CustomerId: 1, CardNo: "Hello", SavingProductId: 1, }) if err != nil { t.Fatalf("SayHello failed: %v", err) } fmt.Printf("Response: %+v", resp) // Test for output here.谢谢
1 回答
梦里花落0921
TA贡献1772条经验 获得超6个赞
所以,你有方法:
GetSavingAccount(*context.Context,uint64,uint64,string,int64)
你嘲笑过:
GetSavingAccount(*context.Context,uint64,uint64,string,int)
你在最后一个参数上有区别,你的问题是int64和int,所以你需要:
mockAccountService.On("GetSavingAccount", mock.Anything, a, a, "Hello", int64(1)).Return(...})
对于上下文参数总是使用mock.Anything,它比模拟上下文更容易。小心 int/int64/int32 和其他类型,指针/结构参数也是如此。
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消