2

所以我正在使用嘲弄作证在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.

但我得到这样的错误:


panic: 

mock: Unexpected Method Call
-----------------------------

GetSavingAccount(*context.valueCtx,uint64,uint64,string,int64)
                0: &context.valueCtx{Context:(*context.valueCtx)(0xc000115260), key:grpc.streamKey{}, val:(*transport.Stream)(0xc0004a2200)}
                1: 0x1
                2: 0x1
                3: "Hello"
                4: 1

The closest call I have is: 

GetSavingAccount(mock.AnythingOfTypeArgument,uint64,uint64,string,int)
                0: "&context.ValueCtx"
                1: 0x1
                2: 0x1
                3: "Hello"
                4: 1

我应该将什么值传递给 mock context.Background()

我试过了mock.AnythingOfType("&context.emptyCtx")mock.Anything不行

谢谢

编辑:

我试过了

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) GetSavingAccount(ctx context.Context, customerID, id uint64, cardNo string, savingProductId int64) (*models.SavingAccount, error)

4

1 回答 1

4

所以,你有方法:

GetSavingAccount(*context.Context,uint64,uint64,string,int64)

你嘲笑过:

GetSavingAccount(*context.Context,uint64,uint64,string,int)

你在最后一个参数上有区别,你的问题是int64int,所以你需要:

mockAccountService.On("GetSavingAccount", mock.Anything, a, a, "Hello", int64(1)).Return(...})

对于上下文参数总是使用mock.Anything,它比模拟上下文更容易。小心 int/int64/int32 和其他类型,指针/结构参数也是如此。

于 2020-07-02T12:46:22.330 回答