ZhangZhihui's Blog  

 

复制代码
func TestLoginUserAPI(t *testing.T) {
    user, password := randomUser(t)

    testCases := []struct {
        name string
        body gin.H
        buildStubs func(store *mockdb.MockStore)
        checkResponse func(recorder *httptest.ResponseRecorder)
    } {
        {
            name: "OK",
            body: gin.H{
                "username": user.Username,
                "password": password,
            },
            buildStubs: func(store *mockdb.MockStore) {
                store.EXPECT().
                    GetUser(gomock.Any(), gomock.Eq(user.Username)).
                    Times(1).
                    Return(user, nil)
            },
            checkResponse: func(recorder *httptest.ResponseRecorder) {
                require.Equal(t, http.StatusOK, recorder.Code)
            },
        },
复制代码

This test case failed, because in the function loginUser that is tested there are two calls by the store:

......
user, err := server.store.GetUser(ctx, req.Username)
......
session, err := server.store.CreateSession(ctx, db.CreateSessionParams{
......

but there's only one store.EXPECT() in the test case. 

Add one for the other function call and the test case will pass:

复制代码
func TestLoginUserAPI(t *testing.T) {
    user, password := randomUser(t)

    testCases := []struct {
        name string
        body gin.H
        buildStubs func(store *mockdb.MockStore)
        checkResponse func(recorder *httptest.ResponseRecorder)
    } {
        {
            name: "OK",
            body: gin.H{
                "username": user.Username,
                "password": password,
            },
            buildStubs: func(store *mockdb.MockStore) {
                store.EXPECT().
                    GetUser(gomock.Any(), gomock.Eq(user.Username)).
                    Times(1).
                    Return(user, nil)
                store.EXPECT().
                    CreateSession(gomock.Any(), gomock.Any()).
                    Times(1)
            },
            checkResponse: func(recorder *httptest.ResponseRecorder) {
                require.Equal(t, http.StatusOK, recorder.Code)
            },
        },
复制代码

 

posted on   ZhangZhihuiAAA  阅读(3)  评论(0编辑  收藏  举报
编辑推荐:
· 时间轮在 Netty , Kafka 中的设计与实现
· MySQL 优化利器 SHOW PROFILE 的实现原理
· 在.NET Core中使用异步多线程高效率的处理大量数据
· 聊一聊 C#前台线程 如何阻塞程序退出
· 几种数据库优化技巧
阅读排行:
· 跟着 8.6k Star 的开源数据库,搞 RAG!
· 夜莺 v8 第一个版本来了,开始做有意思的功能了
· 3款.NET开源、功能强大的通讯调试工具,效率提升利器!
· 推荐一个C#轻量级矢量图形库
· .NET 9 增强 OpenAPI 规范,不再内置swagger
历史上的今天:
2021-12-21 HTML - Hide an element
 
点击右上角即可分享
微信分享提示