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
1
vote
2 answers

Golang how to test function that return channel type?

I try to test function StartP, Expect that Start() should be called 1 times, Done() should be called 1 times but I have trouble that test will block when run this step <-ps.Done() I expect <-ps.Done() return nil How can I test function that return…
Y. Ryan
  • 21
  • 5
1
vote
0 answers

how to gomock a server streaming grpc method?

I tried using the following code but the client hangs and never receives from the mock server. I thought this part: Do(rs.EXPECT().Send(&proto.Response{Result: "steve"})would make the mock server response to the client, it never worked out. For a…
Steve Wu
  • 143
  • 11
1
vote
0 answers

How to recover argument sent to mock with Ginkgo

I am working with /onsi/ginkgo/v2 v2.1.4 and /golang/mock v1.6.0 in Golang v1.19.2 And I want to retrieve the argument that was sent to the mock's InsertOneProspecto function. ctrl = gomock.NewController(GinkgoT()) mockDB =…
Kaltresian
  • 961
  • 3
  • 14
  • 32
1
vote
0 answers

How to mock Grpc Client inside GRPC server implementation for integration tests running with Docker

In order to do E2E integration testing, I am using docker container to spin up the API service. datalake_api: build: context: ../../../ dockerfile: go.Dockerfile Inside API service main.go file, I am initialising another client…
Anshum17
  • 201
  • 3
  • 8
1
vote
1 answer

Gomock same method twice with different input

Note: Not a duplicate of Mock interface method twice with different input and output using testify - Different library. I am using the github.com/golang/mock/gomock library to mock an HTTP client interface in order to test the behaviour of my code.…
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
1
vote
0 answers

Common Bazel gomock rule

Is there a way to declare a gomock rule in one library and reference it in another? The instructions for gomock work for interfaces declared in the same package. For example: backend/service/db/BUILD.bazel: # Contains StargateQueryExecutor…
1
vote
1 answer

Expected value is changed in Return Method of mockgen during its call

I am new to Go and recently, I am trying to write test cases using gomock package. I encountered a strange problem. I am trying to write test case for GetUsers whose implementation is func (ctrl *HttpController) GetUsers(w http.ResponseWriter, r…
1
vote
2 answers

How to mock net.Interface

I'm trying to mock net.Interface in Go, I use net.Interfaces() and I want to have a fixed return. But net.Interface is not an interface, so I can't mock it with gomock. Maybe I'm wrong in the way I test. Here is the method I want to test: const…
WinXaito
  • 41
  • 6
1
vote
1 answer

How to use gomock (or similar) to mock/verify calls to the DB?

Go here, using gorm to or/map to the DB (PSQL). I have the following code: package dbstuff import ( "errors" "github.com/google/uuid" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/postgres" ) type OrderPersister struct…
hotmeatballsoup
  • 385
  • 6
  • 58
  • 136
1
vote
0 answers

Gomock SetArg() panicking

I'm trying to use gomock to mock an interface called SuccessHandler to test a function. The interfaces I have: type Item interface { SetResults(results string) } type SuccessHandler interface { HandleSuccess(item Item) error } And an…
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
1
vote
1 answer

How to properly test a controller class in Go

I'm using gomock to generate a business layer and mock it's methods results. So far, I can't get the test to pass, it is saying that "want" and "got" values are different I'm passing a json representation of my object to strings.NewReader and the…
freitas
  • 313
  • 6
  • 16
1
vote
1 answer

gomock, Go,mango package ,MongoMock

I am trying to mock the below method using gomock func GetS(tenantName string) (*mgo.Session, error) { ctx := apiContext.TContext{} url, err := connectionURLList.get(tenantName) if err != nil { log.GenericWarning(ctx, …
1
vote
0 answers

How to mock a method that returns a struct?

Here's a little golang program that connects to localhost via ssh and does some sftp-like operations. It works fine but I would like to make the doTheWork() function more testable. I've read up on using gomock to create mocks for interfaces. So I've…
Dan Tenenbaum
  • 1,809
  • 3
  • 23
  • 35
0
votes
1 answer

How to use gomock to make a mocked function return different results on subsequent calls?

I am using gomock to mock an http function that is called within the function under test. While it works great for one call it seems to call the first function again when I want to return a different result on the second call (see code below). The…
124141251
  • 31
  • 5
0
votes
0 answers

sqlmock "expecting database transaction Begin"

I am trying to do unit testing with sqlmock. I have the mock.ExpectBegin() and the mock.ExpectCommit(), but I keep getting this error: there is a remaining expectation which was not matched: ExpectedBegin => expecting database transaction Begin I'm…