Unit testing 戈朗模拟情境恐慌

Unit testing 戈朗模拟情境恐慌,unit-testing,go,mockery,testify,Unit Testing,Go,Mockery,Testify,所以我在golang使用和进行单元测试 测试代码如下所示: const bufSize = 1024 * 1024 var lis *bufconn.Listener var mockAccountService = &mocks.AccountService{} func init() { lis = bufconn.Listen(bufSize) s := grpc.NewServer() RegisterAccountManagementHandler

所以我在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.
但我得到的错误如下:

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.
我应该向mock context.Background传递什么值

我试过mock.AnythingOfType&context.emptyCtx,mock.AnythingOfType&context.emptyCtx,mock

多谢各位

编辑:

我试过了

mockAccountService.On("GetSavingAccount", context.Background(), a, a, "Hello", 1).Return(...})
并获得:

GetSavingAccount(*context.valueCtx,uint64,uint64,string,int64)
                0: &context.valueCtx{Context:(*context.valueCtx)(0xc000021290), key:grpc.streamKey{}, val:(*transport.Stream)(0xc000522100)}
                ...
The closest call I have is: 

GetSavingAccount(*context.emptyCtx,uint64,uint64,string,int)
                0: (*context.emptyCtx)(0xc00002a080)
                ...
GetSavingAccount方法的方法定义为:

func a*accountService GetSavingAccountctx context.context,customerID,id uint64,cardNo string,savingProductId int64*models.SavingAccount,error

因此,您有一个方法:

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和其他类型,也要注意指针/结构参数。

因此,您有以下方法:

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和其他类型,也要注意指针/结构参数。

如何注入mockAccountService供客户端使用?请添加,添加GetSavingAccount方法的函数定义,以查看参数和返回类型。还有,mock.anythingsforcontext,这里应该是另一个问题,而不是context.Background。你不需要mock context.Background。只需使用context.Background。“你为什么认为你想要一个嘲笑?”Flimzy我试过你的建议,结果是我edit@VasileRazdalovschi我编辑我的问题,为GetSavingAccount方法添加函数定义,并完成测试文件如何注入mockAccountService供客户端使用?请添加,为GetSavingAccount方法添加函数定义,查看参数和返回类型。还有,mock.anythingsforcontext,这里应该是另一个问题,而不是context.Background。你不需要mock context.Background。只需使用context.Background。“你为什么认为你想要一个嘲笑?”Flimzy我试过你的建议,结果是我edit@VasileRazdalovschi我编辑我的问题,为GetSavingAccount方法添加函数定义并完成测试file@DimasAnggaSaputra另外,如果您不喜欢mock.Anything,您可以这样做:ctx:=context.Background service.Mockmethod,ctx,。。。客户端。Doctx。。。你将跳过这个模拟。任何东西,但它有点难看,取决于你的品味:@DimasAnggaSaputra,如果你不喜欢模拟。任何东西,你都可以做:ctx:=context.Background service.Mockmethod,ctx,。。。客户端。Doctx。。。你将跳过这个模拟。任何东西,但它有点难看,取决于你的品味: