How do you set the return value of a mocked function?
Asked Answered
E

1

5

I am using gomock to create mock objects for unit testing. The following gives the mock object a method called GetQuestionById and tells the mock controller to expect the method to be called with argument 1:

gw.EXPECT().GetQuestionById(1)

But how do I specify that the mocked method should return a particular value?

Exaggerated answered 26/10, 2013 at 4:53 Comment(0)
E
7

When you call gw.EXPECT().GetQuestionById(1), it ends up calling the method RecordCall on the mock controller. RecordCall returns a Call, and Call has a method called Return that does exactly what you want:

gw.EXPECT().GetQuestionById(1).Return(Question{1, "Foo"})
Exaggerated answered 26/10, 2013 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.