Questions tagged [gomock]

GoMock is a mocking framework for the Go programming language.

GoMock integrates well with Go's built-in testing package, but can be used in other contexts too.

68 questions
3
votes
1 answer

Gomock unexposed interface as argument

I'm using gomock (source mode) and wish to mock a piece of code that looks something like: type foo interface { MethodA() int } type Boo interface { MethodB(f foo) string } where the unexported foo interface is used as an argument in…
kito
  • 41
  • 7
3
votes
4 answers

gomock missing call(s)

I am trying to mock the below method using gomock func (w *writer) Publish(vacancies []model.Vacancy) error { ... if _, err = w.conn.WriteMessages(msg); err != nil { return fmt.Errorf("failed to write message: %w", err) …
Ramil Kuvatov
  • 43
  • 1
  • 1
  • 4
3
votes
2 answers

Unexpected call to mock ( gomock) gRPC

I have gRPC service (say svc1) which invokes another gRPC service, using its gRPC stub (say svc2_client). I have generated the client mock for svc2_client. Unit tests as per documentation are working fine, i.e. svc2_client is tested using the…
nukul
  • 113
  • 1
  • 4
  • 10
3
votes
1 answer

How to test call expectation in Go

I have a class MyClass that I want to test. MyClass has a void method that calls an inner server to do something. func (d *MyClass) SendToServer(args) do stuff.... server.Send(myMessage) I want to mock the server call Send, but since the method…
user844541
  • 2,868
  • 5
  • 32
  • 60
2
votes
1 answer

gomock: because: there are no expected calls of the method "Pod" for that receiver

I am trying to mock mysql, but occur error: "because: there are no expected calls of the method "Pod" for that receiver. " I confirmed that I have generated the Pod method with the Mockgen tool, Below is my code func TestPodService_Create(t…
Suzijian
  • 21
  • 1
  • 3
2
votes
1 answer

How to mock only one method of an interface

I am struggling to understand mocking in Go (am looking for something related to Mockito.spy equivalent of java in Go). Let's say I have an interface in Go with 5 methods. But the piece of code that I want to test has references to only two methods.…
Mozhi
  • 757
  • 1
  • 11
  • 28
2
votes
1 answer

How to test that a function was called in a goroutine?

I'd like to make sure that we're starting a goroutine by calling a function with the right arguments. For example: func MyTest(t *testing.T) { service.EXPECT().MyMockFunc(1) service.MyFunc() } func MyFunc() { go MyMockFunc(1) } When I…
yndolok
  • 5,197
  • 2
  • 42
  • 46
2
votes
2 answers

Is there any option to use something similar to mockito argument captor?

we are using gopkg.in/mgo.v2/bson to talk with mongo, and its API populates passed structures instead returning results, for example: func (p *Pipe) One(result interface{}) error {... Problems occurs when I want to mock / test code which is using…
hi_my_name_is
  • 4,894
  • 3
  • 34
  • 50
2
votes
1 answer

Using mockgen in reflect mode

I am trying to write some tests for an existing library but cannot get gomock to work I would like to test behaviour if rand.Read fails. Here is an example of what I would like to test. I would like to see the log.Error line executing in test…
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
1
vote
0 answers

How to create Unit test cases for “GetListOfUsers()” function of “UserService.go“ file. Facing issue in creating dummy aerospike.Recordset object

How to create Unit test cases for “GetListOfUsers()” function of “UserService.go“ file. Facing issue in creating dummy aerospike.Recordset object this is the test file Sample testfile func (suite *UserServiceTestSuite) TestGetListOfUsers()…
A_Gour
  • 21
  • 4
1
vote
1 answer

mockgen not generating mock for interface

Project structure ~/vscodego - has go.mod and go.sum and one .go file ~/vscodego/testfiles has heap1_test.go test1_file_test.go Under the directory ~vscodego/testfiles, trying to generate mock for interface that is in the file…
curiousengineer
  • 2,196
  • 5
  • 40
  • 59
1
vote
1 answer

Unit testing sql.DB/Tx calls in Go with gomock

It’s already the 3rd day I’m thinking about this problem but without any solution. I need to unit test my Tx manager - check that in all combinations rollback() and commit() methods all called in proper sequence. sql.DB has the method BeginTx which…
sadensmol
  • 93
  • 1
  • 7
1
vote
2 answers

I get this error in my test; "aborting test due to missing call(s)" using gomock

Here is my test: func TestCashDepositAPI(t *testing.T) { .... testCases := []struct { name string // name of the test scenario - ok, notfound etc reqBody gin.H setupAuth func(t *testing.T, request…
ramshadows
  • 11
  • 1
1
vote
0 answers

How do you gomock test a method that is importing another package in a closure function?

Here is my go project code structure with implementation: --- Folder structure --- gitlab.com/myproject/ modules/ itemcategory/ - itemcategorymanager.go - itemcategorymanager_test.go dataacess/ model/ -…
Victor Vic
  • 23
  • 3
1
vote
1 answer

Unit testing S3 PreSigned API

Im using s3's GetObjectRequest API to get the Request and use it to call Presign API. type S3Client struct { client s3iface.S3API } type S3API interface { PutObject(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) …
rsundhar
  • 115
  • 5