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) }, },
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 时间轮在 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