Best Mock code snippet using gomock.SetArg
proposal_strategy_test.go
Source:proposal_strategy_test.go
...18 mockStub.EXPECT().CurrentCaller().Return(adminAddr).AnyTimes()19 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.String(), "IsAnyAvailableAdmin", pb.String(noAdminAddr), pb.String(string(GovernanceAdmin))).Return(boltvm.Success([]byte(FALSE))).AnyTimes()20 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.String(), "IsAnyAvailableAdmin", pb.String(adminAddr), pb.String(string(GovernanceAdmin))).Return(boltvm.Success([]byte(TRUE))).AnyTimes()21 // 0: updating22 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[7]).Return(true).Times(1)23 // 0: available24 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[0]).Return(true).AnyTimes()25 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.Address().String(), "GetRolesByType", pb.String(string(GovernanceAdmin))).Return(boltvm.Error("", "crossinvoke GetRolesByType error")).Times(1)26 roles := make([]*Role, 0)27 roles = append(roles, &Role{28 Status: governance.GovernanceAvailable,29 })30 rolesData, err := json.Marshal(roles)31 assert.Nil(t, err)32 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.Address().String(), "GetRolesByType", pb.String(string(GovernanceAdmin))).Return(boltvm.Success(rolesData)).AnyTimes()33 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("SubmitProposal"),34 gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Error("", "SubmitProposal error")).Times(1)35 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("SubmitProposal"),36 gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()37 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("ZeroPermission"),38 gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()39 mockStub.EXPECT().PostEvent(gomock.Any(), gomock.Any()).AnyTimes()40 mockStub.EXPECT().Get(gomock.Any()).Return(true, nil).AnyTimes()41 mockStub.EXPECT().Logger().Return(log.NewWithModule("contracts")).AnyTimes()42 mockStub.EXPECT().Caller().Return(adminAddr).AnyTimes()43 mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).Return().AnyTimes()44 // promission error45 res := g.UpdateProposalStrategy(strategies[0].Module, string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")46 assert.False(t, res.Ok, string(res.Result))47 // update updating strategy error48 res = g.UpdateProposalStrategy(strategies[0].Module, string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")49 assert.False(t, res.Ok, string(res.Result))50 // not update error51 res = g.UpdateProposalStrategy(strategies[0].Module, string(SimpleMajority), "a==t", "123")52 assert.False(t, res.Ok, string(res.Result))53 // GetRolesByType error54 res = g.UpdateProposalStrategy(strategies[0].Module, string(ZeroPermission), "", "123")55 assert.False(t, res.Ok, string(res.Result))56 // illegal strategy info error57 res = g.UpdateProposalStrategy(strategies[0].Module, "", "", "123")58 assert.False(t, res.Ok, string(res.Result))59 // submit proposal error60 res = g.UpdateProposalStrategy(strategies[0].Module, string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")61 assert.False(t, res.Ok, string(res.Result))62 // ok63 res = g.UpdateProposalStrategy(strategies[0].Module, string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")64 assert.True(t, res.Ok, string(res.Result))65}66func TestGovStrategy_UpdateAllProposalStrategy(t *testing.T) {67 g, mockStub, strategies, _ := proposalStrategyPrepare(t)68 mockStub.EXPECT().CurrentCaller().Return(noAdminAddr).Times(1)69 mockStub.EXPECT().CurrentCaller().Return(adminAddr).AnyTimes()70 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.String(), "IsAnyAvailableAdmin", pb.String(noAdminAddr), pb.String(string(GovernanceAdmin))).Return(boltvm.Success([]byte(FALSE))).AnyTimes()71 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.String(), "IsAnyAvailableAdmin", pb.String(adminAddr), pb.String(string(GovernanceAdmin))).Return(boltvm.Success([]byte(TRUE))).AnyTimes()72 // 0: updating73 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[7]).Return(true).Times(1)74 // 0: available75 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[0]).Return(true).AnyTimes()76 // 1: available77 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[1].Module), gomock.Any()).SetArg(1, *strategies[1]).Return(true).AnyTimes()78 // 2: available79 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[2].Module), gomock.Any()).SetArg(1, *strategies[2]).Return(true).AnyTimes()80 // 3: available81 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[3].Module), gomock.Any()).SetArg(1, *strategies[3]).Return(true).AnyTimes()82 // 4: available83 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[4].Module), gomock.Any()).SetArg(1, *strategies[4]).Return(true).AnyTimes()84 // 5: available85 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[5].Module), gomock.Any()).SetArg(1, *strategies[5]).Return(true).AnyTimes()86 // 6: available87 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[6].Module), gomock.Any()).SetArg(1, *strategies[6]).Return(true).AnyTimes()88 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.Address().String(), "GetRolesByType", pb.String(string(GovernanceAdmin))).Return(boltvm.Error("", "crossinvoke GetRolesByType error")).Times(1)89 roles := make([]*Role, 0)90 roles = append(roles, &Role{91 Status: governance.GovernanceAvailable,92 })93 rolesData, err := json.Marshal(roles)94 assert.Nil(t, err)95 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.Address().String(), "GetRolesByType", pb.String(string(GovernanceAdmin))).Return(boltvm.Success(rolesData)).AnyTimes()96 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("SubmitProposal"),97 gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Error("", "SubmitProposal error")).Times(1)98 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("SubmitProposal"),99 gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()100 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("ZeroPermission"),101 gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()102 mockStub.EXPECT().PostEvent(gomock.Any(), gomock.Any()).AnyTimes()103 mockStub.EXPECT().Get(gomock.Any()).Return(true, nil).AnyTimes()104 mockStub.EXPECT().Logger().Return(log.NewWithModule("contracts")).AnyTimes()105 mockStub.EXPECT().Caller().Return(adminAddr).AnyTimes()106 mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).Return().AnyTimes()107 // promission error108 res := g.UpdateAllProposalStrategy(string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")109 assert.False(t, res.Ok)110 // update updating strategy error111 res = g.UpdateAllProposalStrategy(string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")112 assert.False(t, res.Ok)113 // not update error114 res = g.UpdateAllProposalStrategy(string(SimpleMajority), "a==t", "123")115 assert.False(t, res.Ok)116 // GetRolesByType error117 res = g.UpdateAllProposalStrategy(string(ZeroPermission), "", "123")118 assert.False(t, res.Ok, string(res.Result))119 // illegal strategy info error120 res = g.UpdateAllProposalStrategy("", repo.DefaultSimpleMajorityExpression, "123")121 assert.False(t, res.Ok)122 // submit proposal error123 res = g.UpdateAllProposalStrategy(string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")124 assert.False(t, res.Ok)125 // ok126 res = g.UpdateAllProposalStrategy(string(SimpleMajority), repo.DefaultSimpleMajorityExpression, "123")127 assert.True(t, res.Ok)128}129func TestGovStrategy_UpdateProposalStrategyByRolesChange(t *testing.T) {130 g, mockStub, strategies, _ := proposalStrategyPrepare(t)131 mockStub.EXPECT().CurrentCaller().Return(noAdminAddr).Times(1)132 mockStub.EXPECT().CurrentCaller().Return(constant.RoleContractAddr.Address().String()).AnyTimes()133 // 0: updating134 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[7]).Return(true).AnyTimes()135 // 1: available136 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[1].Module), gomock.Any()).SetArg(1, *strategies[1]).Return(true).AnyTimes()137 // 2: available138 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[2].Module), gomock.Any()).SetArg(1, *strategies[2]).Return(true).AnyTimes()139 // 3: available140 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[3].Module), gomock.Any()).SetArg(1, *strategies[3]).Return(true).AnyTimes()141 // 4: available142 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[4].Module), gomock.Any()).SetArg(1, *strategies[4]).Return(true).AnyTimes()143 // 5: available144 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[5].Module), gomock.Any()).SetArg(1, *strategies[5]).Return(true).AnyTimes()145 // 6: available/a==5146 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[6].Module), gomock.Any()).SetArg(1, *strategies[14]).Return(true).AnyTimes()147 mockStub.EXPECT().Logger().Return(log.NewWithModule("contracts")).AnyTimes()148 mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).Return().AnyTimes()149 // promission error150 res := g.UpdateProposalStrategyByRolesChange(4)151 assert.False(t, res.Ok)152 res = g.UpdateProposalStrategyByRolesChange(4)153 assert.True(t, res.Ok)154}155func TestGovStrategy_Manage(t *testing.T) {156 g, mockStub, strategies, _ := proposalStrategyPrepare(t)157 mockStub.EXPECT().CurrentCaller().Return(noAdminAddr).Times(1)158 mockStub.EXPECT().CurrentCaller().Return(constant.GovernanceContractAddr.Address().String()).AnyTimes()159 // 0: available160 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[0]).Return(true).Times(2)161 // 0: updating162 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[7]).Return(true).AnyTimes()163 // 1: updating164 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[1].Module), gomock.Any()).SetArg(1, *strategies[8]).Return(true).AnyTimes()165 // 2: updating166 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[2].Module), gomock.Any()).SetArg(1, *strategies[9]).Return(true).AnyTimes()167 // 3: updating168 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[3].Module), gomock.Any()).SetArg(1, *strategies[10]).Return(true).AnyTimes()169 // 4: updating170 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[4].Module), gomock.Any()).SetArg(1, *strategies[11]).Return(true).AnyTimes()171 // 5: updating172 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[5].Module), gomock.Any()).SetArg(1, *strategies[12]).Return(true).AnyTimes()173 // 6: updating174 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[6].Module), gomock.Any()).SetArg(1, *strategies[13]).Return(true).AnyTimes()175 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.Address().String(), "GetRolesByType", pb.String(string(GovernanceAdmin))).Return(boltvm.Error("", "crossinvoke GetRolesByType error")).Times(1)176 roles := make([]*Role, 0)177 roles = append(roles, &Role{178 Status: governance.GovernanceAvailable,179 })180 rolesData, err := json.Marshal(roles)181 assert.Nil(t, err)182 mockStub.EXPECT().CrossInvoke(constant.RoleContractAddr.Address().String(), "GetRolesByType", pb.String(string(GovernanceAdmin))).Return(boltvm.Success(rolesData)).AnyTimes()183 mockStub.EXPECT().CrossInvoke(gomock.Eq(constant.GovernanceContractAddr.Address().String()), gomock.Eq("ZeroPermission"),184 gomock.Any()).Return(boltvm.Success(nil)).AnyTimes()185 mockStub.EXPECT().PostEvent(gomock.Any(), gomock.Any()).AnyTimes()186 mockStub.EXPECT().Get(gomock.Any()).Return(true, nil).AnyTimes()187 mockStub.EXPECT().Logger().Return(log.NewWithModule("contracts")).AnyTimes()188 mockStub.EXPECT().Caller().Return(adminAddr).AnyTimes()189 mockStub.EXPECT().SetObject(gomock.Any(), gomock.Any()).Return().AnyTimes()190 updateStrategyInfo := map[string]UpdateStrategyInfo{191 strategies[0].Module: UpdateStrategyInfo{192 Typ: UpdateInfo{193 OldInfo: ZeroPermission,194 NewInfo: SimpleMajority,195 IsEdit: true,196 },197 Extra: UpdateInfo{198 OldInfo: "",199 NewInfo: repo.DefaultSimpleMajorityExpression,200 IsEdit: true,201 },202 },203 }204 extra, err := json.Marshal(updateStrategyInfo)205 assert.Nil(t, err)206 updateStrategyInfo1 := map[string]UpdateStrategyInfo{207 strategies[0].Module: UpdateStrategyInfo{208 Typ: UpdateInfo{209 OldInfo: ZeroPermission,210 NewInfo: SimpleMajority,211 IsEdit: true,212 },213 Extra: UpdateInfo{214 OldInfo: "",215 NewInfo: "a==4",216 IsEdit: true,217 },218 },219 }220 extra1, err := json.Marshal(updateStrategyInfo1)221 assert.Nil(t, err)222 updateAllStrategyInfo := map[string]UpdateStrategyInfo{}223 for _, mgr := range mgrs {224 updateAllStrategyInfo[mgr] = UpdateStrategyInfo{225 Typ: UpdateInfo{226 OldInfo: SimpleMajority,227 NewInfo: ZeroPermission,228 IsEdit: true,229 },230 Extra: UpdateInfo{231 OldInfo: repo.DefaultSimpleMajorityExpression,232 NewInfo: "",233 IsEdit: true,234 },235 }236 }237 allExtra, err := json.Marshal(updateAllStrategyInfo)238 assert.Nil(t, err)239 // promission error240 res := g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), strategies[0].Module, extra)241 assert.False(t, res.Ok)242 // not all mgr change status error243 res = g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), strategies[0].Module, extra)244 assert.False(t, res.Ok)245 // all mgr change status error246 res = g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), repo.AllMgr, extra)247 assert.False(t, res.Ok)248 // GetRolesByType error249 res = g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), strategies[0].Module, extra)250 assert.False(t, res.Ok)251 // update to default252 res = g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), strategies[0].Module, extra1)253 assert.True(t, res.Ok)254 res = g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), strategies[0].Module, extra)255 assert.True(t, res.Ok)256 res = g.Manage(string(governance.EventUpdate), BallotApprove, string(governance.GovernanceAvailable), repo.AllMgr, allExtra)257 assert.True(t, res.Ok)258}259func TestGovStrategy_GetAllProposalStrategy(t *testing.T) {260 g, mockStub, strategies, _ := proposalStrategyPrepare(t)261 // 0: not set262 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).Return(false).Times(1)263 // 0: available264 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[0]).Return(true).AnyTimes()265 // 1: available266 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[1].Module), gomock.Any()).SetArg(1, *strategies[1]).Return(true).AnyTimes()267 // 2: available268 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[2].Module), gomock.Any()).SetArg(1, *strategies[2]).Return(true).AnyTimes()269 // 3: available270 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[3].Module), gomock.Any()).SetArg(1, *strategies[3]).Return(true).AnyTimes()271 // 4: available272 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[4].Module), gomock.Any()).SetArg(1, *strategies[4]).Return(true).AnyTimes()273 // 5: available274 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[5].Module), gomock.Any()).SetArg(1, *strategies[5]).Return(true).AnyTimes()275 // 6: available276 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[6].Module), gomock.Any()).SetArg(1, *strategies[6]).Return(true).AnyTimes()277 res := g.GetAllProposalStrategy()278 assert.True(t, res.Ok)279 res = g.GetAllProposalStrategy()280 assert.True(t, res.Ok)281}282func TestGovStrategy_GetProposalStrategy(t *testing.T) {283 g, mockStub, strategies, _ := proposalStrategyPrepare(t)284 // 0: not set285 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).Return(false).Times(1)286 // 0: available287 mockStub.EXPECT().GetObject(ProposalStrategyKey(strategies[0].Module), gomock.Any()).SetArg(1, *strategies[0]).Return(true).AnyTimes()288 res := g.GetProposalStrategy(strategies[0].Module)289 assert.True(t, res.Ok)290 res = g.GetProposalStrategy(strategies[0].Module)291 assert.True(t, res.Ok)292}293func proposalStrategyPrepare(t *testing.T) (*GovStrategy, *mock_stub.MockStub, []*ProposalStrategy, [][]byte) {294 mockCtl := gomock.NewController(t)295 mockStub := mock_stub.NewMockStub(mockCtl)296 g := &GovStrategy{297 Stub: mockStub,298 }299 strategies := make([]*ProposalStrategy, 0)300 strategiesData := make([][]byte, 0)301 for i := 0; i < 7; i++ {...
erc20_rpc_test.go
Source:erc20_rpc_test.go
...90 c.ERC20Transfer(context)91 context.Set("p", fmt.Sprintf("%s,%s,to,amount,gasPrice,gasLimit", contractAddr, from))92 c.ERC20Transfer(context)93 client = NewMockRpcClient(ctrl)94 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)95 context.Set("p", fmt.Sprintf("%s,%s,%s,amount,gasPrice,gasLimit", contractAddr, from, to))96 c.ERC20Transfer(context)97 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)98 context.Set("p", fmt.Sprintf("%s,%s,%s,%v,gasPrice,gasLimit", contractAddr, from, to, "10"))99 c.ERC20Transfer(context)100 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)101 context.Set("p", fmt.Sprintf("%s,%s,%s,%v,%v,gasLimit", contractAddr, from, to, "10", "10wu"))102 c.ERC20Transfer(context)103 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)104 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)105 context.Set("p", fmt.Sprintf("%s,%s,%s,%v,%v,%v", contractAddr, from, to, "10", "10wu", "100"))106 c.ERC20Transfer(context)107 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)108 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)109 c.ERC20Transfer(context)110 }111 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20Transfer"}))112 client = nil113}114func TestRpcCaller_ERC20TransferFrom(t *testing.T) {115 ctrl := gomock.NewController(t)116 defer ctrl.Finish()117 app := getRpcTestApp()118 app.Action = func(context *cli.Context) {119 c := &rpcCaller{}120 c.ERC20TransferFrom(context)121 }122 assert.NoError(t, app.Run([]string{os.Args[0]}))123 app.Action = func(context *cli.Context) {124 c := &rpcCaller{}125 context.Set("p", "")126 c.ERC20TransferFrom(context)127 context.Set("p", "contractAddr,owner,from,to,amount,gasPrice,gasLimit")128 c.ERC20TransferFrom(context)129 context.Set("p", fmt.Sprintf("%s,owner,from,to,amount,gasPrice,gasLimit", contractAddr))130 c.ERC20TransferFrom(context)131 context.Set("p", fmt.Sprintf("%s,%s,from,to,amount,gasPrice,gasLimit", contractAddr, from))132 c.ERC20TransferFrom(context)133 context.Set("p", fmt.Sprintf("%s,%s,%s,to,amount,gasPrice,gasLimit", contractAddr, from, from))134 c.ERC20TransferFrom(context)135 client = NewMockRpcClient(ctrl)136 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)137 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,amount,gasPrice,gasLimit", contractAddr, from, from, to))138 c.ERC20TransferFrom(context)139 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)140 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,%s,gasPrice,gasLimit", contractAddr, from, from, to, "10"))141 c.ERC20TransferFrom(context)142 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)143 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,%s,%s,gasLimit", contractAddr, from, from, to, "10", "10wu"))144 c.ERC20TransferFrom(context)145 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)146 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)147 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s", contractAddr, from, from, to, "10", "10wu", "100"))148 c.ERC20TransferFrom(context)149 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)150 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)151 c.ERC20TransferFrom(context)152 }153 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20TransferFrom"}))154 client = nil155}156func TestRpcCaller_ERC20Allowance(t *testing.T) {157 ctrl := gomock.NewController(t)158 defer ctrl.Finish()159 app := getRpcTestApp()160 app.Action = func(context *cli.Context) {161 c := &rpcCaller{}162 c.ERC20Allowance(context)163 }164 assert.NoError(t, app.Run([]string{os.Args[0]}))165 app.Action = func(context *cli.Context) {166 c := &rpcCaller{}167 context.Set("p", "")168 c.ERC20Allowance(context)169 context.Set("p", "contractAddr,owner,spender")170 c.ERC20Allowance(context)171 context.Set("p", fmt.Sprintf("%s,owner,spender", contractAddr))172 c.ERC20Allowance(context)173 context.Set("p", fmt.Sprintf("%s,%s,spender", contractAddr, from))174 c.ERC20Allowance(context)175 context.Set("p", fmt.Sprintf("%s,%s,%s", contractAddr, from, to))176 client = NewMockRpcClient(ctrl)177 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)178 c.ERC20Allowance(context)179 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(1e18))180 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)181 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, "dip")182 c.ERC20Allowance(context)183 }184 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20Allowance"}))185 client = nil186}187func TestRpcCaller_ERC20Approve(t *testing.T) {188 ctrl := gomock.NewController(t)189 defer ctrl.Finish()190 app := getRpcTestApp()191 app.Action = func(context *cli.Context) {192 c := &rpcCaller{}193 c.ERC20Approve(context)194 }195 assert.NoError(t, app.Run([]string{os.Args[0]}))196 app.Action = func(context *cli.Context) {197 c := &rpcCaller{}198 context.Set("p", "")199 c.ERC20Approve(context)200 context.Set("p", "contractAddr,owner,to,amount,gasPrice,gasLimit")201 c.ERC20Approve(context)202 context.Set("p", fmt.Sprintf("%s,owner,to,amount,gasPrice,gasLimit", contractAddr))203 c.ERC20Approve(context)204 context.Set("p", fmt.Sprintf("%s,%s,to,amount,gasPrice,gasLimit", contractAddr, from))205 c.ERC20Approve(context)206 client = NewMockRpcClient(ctrl)207 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)208 context.Set("p", fmt.Sprintf("%s,%s,%s,amount,gasPrice,gasLimit", contractAddr, from, to))209 c.ERC20Approve(context)210 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)211 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)212 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,gasPrice,gasLimit", contractAddr, from, to, "10"))213 c.ERC20Approve(context)214 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)215 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(10))216 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,gasPrice,gasLimit", contractAddr, from, to, "10"))217 c.ERC20Approve(context)218 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)219 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(1e5))220 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,gasPrice,gasLimit", contractAddr, from, to, "10"))221 c.ERC20Approve(context)222 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)223 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(1e5))224 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,%s,gasLimit", contractAddr, from, to, "10", "1wu"))225 c.ERC20Approve(context)226 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)227 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(1e5))228 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)229 context.Set("p", fmt.Sprintf("%s,%s,%s,%s,%s,%s", contractAddr, from, to, "10", "1wu", "100"))230 c.ERC20Approve(context)231 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.KUnitDecimalBits)232 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(1e5))233 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)234 c.ERC20Approve(context)235 }236 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20Approve"}))237 client = nil238}239func TestRpcCaller_ERC20TotalSupply(t *testing.T) {240 ctrl := gomock.NewController(t)241 defer ctrl.Finish()242 app := getRpcTestApp()243 app.Action = func(context *cli.Context) {244 c := &rpcCaller{}245 c.ERC20TotalSupply(context)246 }247 assert.NoError(t, app.Run([]string{os.Args[0]}))248 app.Action = func(context *cli.Context) {249 c := &rpcCaller{}250 context.Set("p", "")251 c.ERC20TotalSupply(context)252 context.Set("p", "contractAddr")253 c.ERC20TotalSupply(context)254 context.Set("p", contractAddr)255 client = NewMockRpcClient(ctrl)256 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)257 c.ERC20TotalSupply(context)258 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, big.NewInt(1e18))259 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)260 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, "dip")261 c.ERC20TotalSupply(context)262 }263 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20TotalSupply"}))264 client = nil265}266func TestRpcCaller_ERC20Balance(t *testing.T) {267 ctrl := gomock.NewController(t)268 defer ctrl.Finish()269 app := getRpcTestApp()270 app.Action = func(context *cli.Context) {271 c := &rpcCaller{}272 c.ERC20Balance(context)273 }274 assert.NoError(t, app.Run([]string{os.Args[0]}))275 app.Action = func(context *cli.Context) {276 c := &rpcCaller{}277 context.Set("p", "")278 c.ERC20Balance(context)279 context.Set("p", "contractAddr,owner")280 c.ERC20Balance(context)281 context.Set("p", fmt.Sprintf("%s,owner", contractAddr))282 c.ERC20Balance(context)283 context.Set("p", fmt.Sprintf("%s,%s", contractAddr, from))284 client = NewMockRpcClient(ctrl)285 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)286 c.ERC20Balance(context)287 balance := big.NewInt(1e18)288 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, (*hexutil.Big)(balance))289 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)290 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, "dip")291 c.ERC20Balance(context)292 }293 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20Balance"}))294 client = nil295}296func TestRpcCaller_ERC20TokenName(t *testing.T) {297 ctrl := gomock.NewController(t)298 defer ctrl.Finish()299 app := getRpcTestApp()300 app.Action = func(context *cli.Context) {301 c := &rpcCaller{}302 c.ERC20TokenName(context)303 }304 assert.NoError(t, app.Run([]string{os.Args[0]}))305 app.Action = func(context *cli.Context) {306 c := &rpcCaller{}307 context.Set("p", "")308 c.ERC20TokenName(context)309 context.Set("p", "contractAddr")310 c.ERC20TokenName(context)311 context.Set("p", contractAddr)312 client = NewMockRpcClient(ctrl)313 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)314 c.ERC20TokenName(context)315 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, "name")316 c.ERC20TokenName(context)317 }318 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20TokenName"}))319 client = nil320}321func TestRpcCaller_ERC20TokenSymbol(t *testing.T) {322 ctrl := gomock.NewController(t)323 defer ctrl.Finish()324 app := getRpcTestApp()325 app.Action = func(context *cli.Context) {326 c := &rpcCaller{}327 c.ERC20TokenSymbol(context)328 }329 assert.NoError(t, app.Run([]string{os.Args[0]}))330 app.Action = func(context *cli.Context) {331 c := &rpcCaller{}332 context.Set("p", "")333 c.ERC20TokenSymbol(context)334 context.Set("p", "contractAddr")335 c.ERC20TokenSymbol(context)336 context.Set("p", contractAddr)337 client = NewMockRpcClient(ctrl)338 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)339 c.ERC20TokenSymbol(context)340 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, "symbol")341 c.ERC20TokenSymbol(context)342 }343 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20TokenSymbol"}))344 client = nil345}346func TestRpcCaller_ERC20TokenDecimals(t *testing.T) {347 ctrl := gomock.NewController(t)348 defer ctrl.Finish()349 app := getRpcTestApp()350 app.Action = func(context *cli.Context) {351 c := &rpcCaller{}352 c.ERC20TokenDecimals(context)353 }354 assert.NoError(t, app.Run([]string{os.Args[0]}))355 app.Action = func(context *cli.Context) {356 c := &rpcCaller{}357 context.Set("p", "")358 c.ERC20TokenDecimals(context)359 context.Set("p", "contractAddr")360 c.ERC20TokenDecimals(context)361 context.Set("p", contractAddr)362 client = NewMockRpcClient(ctrl)363 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)364 c.ERC20TokenDecimals(context)365 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, 18)366 c.ERC20TokenDecimals(context)367 }368 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20TokenDecimals"}))369 client = nil370}371func TestRpcCaller_ERC20GetInfo(t *testing.T) {372 ctrl := gomock.NewController(t)373 defer ctrl.Finish()374 app := getRpcTestApp()375 app.Action = func(context *cli.Context) {376 c := &rpcCaller{}377 c.ERC20GetInfo(context)378 }379 assert.NoError(t, app.Run([]string{os.Args[0]}))380 app.Action = func(context *cli.Context) {381 c := &rpcCaller{}382 context.Set("p", "")383 c.ERC20GetInfo(context)384 context.Set("p", "contractAddr")385 c.ERC20GetInfo(context)386 context.Set("p", contractAddr)387 client = NewMockRpcClient(ctrl)388 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).MaxTimes(3)389 c.ERC20GetInfo(context)390 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr)391 c.ERC20GetInfo(context)392 mapInterface := make(map[string]interface{})393 mapInterface["token_total_supply"] = "1000000000000000000"394 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, mapInterface)395 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, consts.DIPDecimalBits)396 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, "dip")397 c.ERC20GetInfo(context)398 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).SetArg(0, mapInterface)399 client.(*MockRpcClient).EXPECT().Call(gomock.Any(), gomock.Any(), gomock.Any()).Return(testErr).MaxTimes(2)400 c.ERC20GetInfo(context)401 }402 assert.NoError(t, app.Run([]string{os.Args[0], "ERC20GetInfo"}))403 client = nil404}...
ocmagenthandler_secret_test.go
Source:ocmagenthandler_secret_test.go
...86 })87 It("updates the secret", func() {88 goldenSecret := buildOCMAgentAccessTokenSecret(testOcmAccessTokenSecretValue, testOcmAgent)89 gomock.InOrder(90 mockClient.EXPECT().Get(gomock.Any(), oahconst.PullSecretNamespacedName, gomock.Any()).Times(1).SetArg(2, testPullSecret),91 mockClient.EXPECT().Get(gomock.Any(), testNamespacedName, gomock.Any()).Times(1).SetArg(2, testSecret),92 mockClient.EXPECT().Update(gomock.Any(), gomock.Any()).Times(1).DoAndReturn(93 func(ctx context.Context, d *corev1.Secret, opts ...client.UpdateOptions) error {94 Expect(d.Data).Should(HaveKey(oahconst.OCMAgentAccessTokenSecretKey))95 Expect(bytes.Compare(d.Data[oahconst.OCMAgentAccessTokenSecretKey], goldenSecret.Data[oahconst.OCMAgentAccessTokenSecretKey])).To(BeZero())96 return nil97 }),98 )99 err := testOcmAgentHandler.ensureAccessTokenSecret(testOcmAgent)100 Expect(err).To(BeNil())101 })102 })103 When("the secret matches what is expected", func() {104 It("does not update the secret", func() {105 gomock.InOrder(106 mockClient.EXPECT().Get(gomock.Any(), oahconst.PullSecretNamespacedName, gomock.Any()).Times(1).SetArg(2, testPullSecret),107 mockClient.EXPECT().Get(gomock.Any(), testNamespacedName, gomock.Any()).Times(1).SetArg(2, testSecret),108 )109 err := testOcmAgentHandler.ensureAccessTokenSecret(testOcmAgent)110 Expect(err).To(BeNil())111 })112 })113 })114 When("the OCM Agent secret does not already exist", func() {115 It("creates the secret", func() {116 notFound := k8serrs.NewNotFound(schema.GroupResource{}, testSecret.Name)117 gomock.InOrder(118 mockClient.EXPECT().Get(gomock.Any(), oahconst.PullSecretNamespacedName, gomock.Any()).Times(1).SetArg(2, testPullSecret),119 mockClient.EXPECT().Get(gomock.Any(), testNamespacedName, gomock.Any()).Times(1).Return(notFound),120 mockClient.EXPECT().Create(gomock.Any(), gomock.Any()).DoAndReturn(121 func(ctx context.Context, d *corev1.Secret, opts ...client.CreateOptions) error {122 Expect(reflect.DeepEqual(d.Data, testSecret.Data)).To(BeTrue())123 Expect(d.ObjectMeta.OwnerReferences[0].Kind).To(Equal("OcmAgent"))124 Expect(*d.ObjectMeta.OwnerReferences[0].BlockOwnerDeletion).To(BeTrue())125 Expect(*d.ObjectMeta.OwnerReferences[0].Controller).To(BeTrue())126 return nil127 }),128 )129 err := testOcmAgentHandler.ensureAccessTokenSecret(testOcmAgent)130 Expect(err).To(BeNil())131 })132 })133 When("the access token secret should be removed", func() {134 When("the secret is already removed", func() {135 It("does nothing", func() {136 notFound := k8serrs.NewNotFound(schema.GroupResource{}, testSecret.Name)137 gomock.InOrder(138 mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(notFound),139 )140 err := testOcmAgentHandler.ensureAccessTokenSecretDeleted(testOcmAgent)141 Expect(err).To(BeNil())142 })143 })144 When("the configmap exists on the cluster", func() {145 It("removes the configmap", func() {146 gomock.InOrder(147 mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).SetArg(2, testSecret),148 mockClient.EXPECT().Delete(gomock.Any(), &testSecret),149 )150 err := testOcmAgentHandler.ensureAccessTokenSecretDeleted(testOcmAgent)151 Expect(err).To(BeNil())152 })153 })154 })155 When("the pull secret can't be found", func() {156 BeforeEach(func() {157 delete(testPullSecret.Data, oahconst.PullSecretKey)158 })159 It("returns an error and sets the metric", func() {160 gomock.InOrder(161 mockClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).SetArg(2, testSecret),162 )163 err := testOcmAgentHandler.ensureAccessTokenSecret(testOcmAgent)164 Expect(err).NotTo(BeNil())165 expectedMetric := `166# HELP ocm_agent_operator_pull_secret_invalid Failed to obtain a valid pull secret167# TYPE ocm_agent_operator_pull_secret_invalid gauge168ocm_agent_operator_pull_secret_invalid{ocmagent_name="test-ocm-agent"} 1169`170 err = testutil.CollectAndCompare(localmetrics.MetricPullSecretInvalid, strings.NewReader(expectedMetric))171 Expect(err).To(BeNil())172 })173 })174 When("the pull secret can be found", func() {175 It("sets the correct metric", func() {176 gomock.InOrder(177 mockClient.EXPECT().Get(gomock.Any(), oahconst.PullSecretNamespacedName, gomock.Any()).Times(1).SetArg(2, testPullSecret),178 mockClient.EXPECT().Get(gomock.Any(), testNamespacedName, gomock.Any()).Times(1).SetArg(2, testSecret),179 )180 err := testOcmAgentHandler.ensureAccessTokenSecret(testOcmAgent)181 Expect(err).To(BeNil())182 expectedMetric := `183# HELP ocm_agent_operator_pull_secret_invalid Failed to obtain a valid pull secret184# TYPE ocm_agent_operator_pull_secret_invalid gauge185ocm_agent_operator_pull_secret_invalid{ocmagent_name="test-ocm-agent"} 0186`187 err = testutil.CollectAndCompare(localmetrics.MetricPullSecretInvalid, strings.NewReader(expectedMetric))188 Expect(err).To(BeNil())189 })190 })191 })192})...
SetArg
Using AI Code Generation
1import (2func TestSetArg(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 m := NewMockMyInterface(ctrl)6 m.EXPECT().SetArg(gomock.Any()).Do(func(arg interface{}) {7 fmt.Println(arg)8 })9 m.SetArg("Hello")10}11import (12func TestSetArg(t *testing.T) {13 ctrl := gomock.NewController(t)14 defer ctrl.Finish()15 m := NewMockMyInterface(ctrl)16 m.EXPECT().SetArg(gomock.Any()).Do(func(arg interface{}) {17 fmt.Println(arg)18 })19 m.SetArg("Hello")20}21import (22func TestSetArg(t *testing.T) {23 ctrl := gomock.NewController(t)24 defer ctrl.Finish()25 m := NewMockMyInterface(ctrl)26 m.EXPECT().SetArg(gomock.Any()).Do(func(arg interface{}) {27 fmt.Println(arg)28 })29 m.SetArg("Hello")30}31import (32func TestSetArg(t *testing.T) {33 ctrl := gomock.NewController(t)34 defer ctrl.Finish()35 m := NewMockMyInterface(ctrl)36 m.EXPECT().SetArg(gomock.Any()).Do(func(arg interface{}) {37 fmt.Println(arg)38 })39 m.SetArg("Hello")40}41import (42func TestSetArg(t *testing.T) {43 ctrl := gomock.NewController(t)44 defer ctrl.Finish()
SetArg
Using AI Code Generation
1import (2func TestMock(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := NewMockFoo(ctrl)6 mock.EXPECT().Bar(gomock.Any()).Return(nil).SetArg(0, "hello")7 err := mock.Bar("world")8 assert.Nil(t, err)9}10import (11func TestMock(t *testing.T) {12 ctrl := gomock.NewController(t)13 defer ctrl.Finish()14 mock := NewMockFoo(ctrl)15 mock.EXPECT().Bar(gomock.Any()).Return(nil).SetArg(0, "hello")16 err := mock.Bar("world")17 assert.Nil(t, err)18}19import (20func TestMock(t *testing.T) {21 ctrl := gomock.NewController(t)22 defer ctrl.Finish()23 mock := NewMockFoo(ctrl)24 mock.EXPECT().Bar(gomock.Any()).Return(nil).SetArg(0, "hello")25 err := mock.Bar("world")26 assert.Nil(t, err)27}28import (29func TestMock(t *testing.T) {30 ctrl := gomock.NewController(t)31 defer ctrl.Finish()32 mock := NewMockFoo(ctrl)33 mock.EXPECT().Bar(gomock.Any()).Return(nil).SetArg(0, "hello")34 err := mock.Bar("world")35 assert.Nil(t, err)36}37import (38func TestMock(t *testing.T) {39 ctrl := gomock.NewController(t)40 defer ctrl.Finish()
SetArg
Using AI Code Generation
1func (m *MockMyInterface) SetArg(arg1 string) {2 m.ctrl.Call(m, "SetArg", arg1)3}4func (m *MockMyInterface) SetArg(arg1 string) {5 m.ctrl.Call(m, "SetArg", arg1)6}7func (m *MockMyInterface) SetArg(arg1 string) {8 m.ctrl.Call(m, "SetArg", arg1)9}10func (m *MockMyInterface) SetArg(arg1 string) {11 m.ctrl.Call(m, "SetArg", arg1)12}13func (m *MockMyInterface) SetArg(arg1 string) {14 m.ctrl.Call(m, "SetArg", arg1)15}16func (m *MockMyInterface) SetArg(arg1 string) {17 m.ctrl.Call(m, "SetArg", arg1)18}19func (m *MockMyInterface) SetArg(arg1 string) {20 m.ctrl.Call(m, "SetArg", arg1)21}22func (m *MockMyInterface) SetArg(arg1 string) {23 m.ctrl.Call(m, "SetArg", arg1)24}25func (m *MockMyInterface) SetArg(arg1 string) {26 m.ctrl.Call(m, "SetArg", arg1)27}28func (m *MockMyInterface) SetArg(arg1 string) {29 m.ctrl.Call(m, "SetArg", arg1)30}31func (m *MockMyInterface) SetArg(arg1 string) {
SetArg
Using AI Code Generation
1import (2func TestSetArg(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mockObj := mock.NewMockInterface(ctrl)6 mockObj.EXPECT().Add(gomock.Any(), gomock.Any()).Do(func(a, b int) {7 fmt.Println("a=", a, "b=", b)8 }).SetArg(1, 10)9 mockObj.Add(1, 2)10}11import (12func TestDoAndReturn(t *testing.T) {13 ctrl := gomock.NewController(t)14 defer ctrl.Finish()15 mockObj := mock.NewMockInterface(ctrl)16 mockObj.EXPECT().Add(gomock.Any(), gomock.Any()).DoAndReturn(func(a, b int) int {17 fmt.Println("a=", a, "b=", b)18 })19 fmt.Println(mockObj.Add(1, 2))20}21import (22func TestDoAndReturn(t *testing.T) {23 ctrl := gomock.NewController(t)24 defer ctrl.Finish()25 mockObj := mock.NewMockInterface(ctrl)26 mockObj.EXPECT().Add(gomock.Any(), gomock.Any()).DoAndReturn(func(a, b int) int {27 fmt.Println("a=", a, "b=", b)28 })29 fmt.Println(mockObj.Add
SetArg
Using AI Code Generation
1import (2func TestMocking(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mock := mocking.NewMockMyInterface(ctrl)6 mock.EXPECT().MyMethod(gomock.Any(), gomock.Any()).Return(100, nil)7 res, err := mock.MyMethod(10, 20)8 fmt.Println(res, err)9}10import (11func TestMocking(t *testing.T) {12 ctrl := gomock.NewController(t)13 defer ctrl.Finish()14 mock := mocking.NewMockMyInterface(ctrl)15 mock.EXPECT().MyMethod(gomock.Any(), gomock.Any()).Do(func(a, b int) {16 fmt.Println("a: ", a, "b: ", b)17 }).Return(100, nil)18 res, err := mock.MyMethod(10, 20)19 fmt.Println(res, err)20}21import (22func TestMocking(t *testing.T) {23 ctrl := gomock.NewController(t)24 defer ctrl.Finish()25 mock := mocking.NewMockMyInterface(ctrl)26 mock.EXPECT().MyMethod(gomock.Any(), gomock.Any()).DoAndReturn(func(a, b int) (int, error) {27 fmt.Println("a: ", a, "b: ", b)28 })29 res, err := mock.MyMethod(10, 20)30 fmt.Println(res, err)31}32import (
SetArg
Using AI Code Generation
1mock.EXPECT().SetArg(0, 42).Return(nil)2mock.EXPECT().SetArg(0, 42).Return(nil)3mock.EXPECT().SetArg(0, 42).Return(nil)4std::ifstream in("data.txt");5std::string data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());6std::cout << data << std::endl;7std::ofstream out("data.txt");8out << "Hello World";9out.close();10 what(): basic_fstream::clear: iostream error11std::ifstream in("data.txt");12std::string data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());13std::cout << data << std::endl;14std::ofstream out("data.txt");15out << "Hello World";16out.close();17 what(): basic_fstream::clear: iostream error18std::ifstream in("data.txt");19std::string data((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());20std::cout << data << std::endl;21std::ofstream out("data.txt");
SetArg
Using AI Code Generation
1import (2func main() {3 ctrl := gomock.NewController(t)4 mock := NewMockMyInterface(ctrl)5 mock.EXPECT().SetArg(gomock.Any()).Return(nil)6 mock.SetArg("hello")7}8import (9func main() {10 ctrl := gomock.NewController(t)11 mock := NewMockMyInterface(ctrl)12 mock.EXPECT().SetArg(gomock.Any()).Return(nil)13 mock.SetArg("hello")14}15import (16func main() {17 ctrl := gomock.NewController(t)18 mock := NewMockMyInterface(ctrl)19 mock.EXPECT().SetArg(gomock.Any()).Return(nil)20 mock.SetArg("hello")21}22import (23func main() {24 ctrl := gomock.NewController(t)25 mock := NewMockMyInterface(ctrl)26 mock.EXPECT().SetArg(gomock.Any()).Return(nil)27 mock.SetArg("hello")28}29import (30func main() {31 ctrl := gomock.NewController(t)32 mock := NewMockMyInterface(ctrl)33 mock.EXPECT().SetArg(gomock.Any()).Return(nil)34 mock.SetArg("hello")35}36import (37func main() {38 ctrl := gomock.NewController(t)39 mock := NewMockMyInterface(ctrl)40 mock.EXPECT().SetArg(gomock.Any()).Return(nil)41 mock.SetArg("hello")42}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!