所以我正在使用嘲弄和作证在golang中进行单元测试
测试代码如下:
const bufSize = 1024 * 1024
var lis *bufconn.Listener
var 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.
谢谢
梦里花落0921
相关分类