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
0
votes
0 answers

Go and gomock.InOrder, subtests not always running

I have a problem with running tests in Go that require a mocked call. I have several test cases with different parameters and for each I also create an expected call for gomock to mock. service := NewService() tests := map[string]struct{ param1…
KronwarsCZ
  • 21
  • 3
0
votes
0 answers

Mockgen/Go Mock on Sqlx db

Structure: . ├── config │ └── config.go ├── internal │ ├── api │ │ └── app.go │ ├── handler │ │ ├── common.go │ │ └── users.go │ ├── model │ │ └── model.go │ └── queries │ └── queries.go └── main.go main.go: package…
Pygirl
  • 12,969
  • 5
  • 30
  • 43
0
votes
2 answers

Golang Context Timeout Not Working Via Test

I have some async calls that runs and I'm setting a timeout for all via the context. ctxWithTimeout, cancel := context.WithTimeout(ctx, getTimeoutDuration()) defer cancel() go func1(ctxWithTimeout, outputChan1, param1) go func2(ctxWithTimeout,…
Itay Gal
  • 10,706
  • 6
  • 36
  • 75
0
votes
3 answers

Golang: Mock an interface method before Init method is called

How can I mock something that gets called in a package init() method? For example: main.go var myService MyService = myservicepkg.New() func init(){ response := myService.get() } func otherMethod(){ //do something } maintest.go func…
cosbor11
  • 14,709
  • 10
  • 54
  • 69
0
votes
0 answers

undefined: gomock.AssignableToTypeOf

I'm getting this error: undefined: gomock.AssignableToTypeOf It is occurring while using function gomock.AssignableToTypeOf of package github.com/golang/mock" This starts occurring when I have Updated versions in the WORKSPACE file of Project, and…
0
votes
2 answers

Struct with an pointer to an Interface mock error

package main import ( "github.com/golang/mock/gomock" "testing" ) type Talker interface { talk() string } type Person struct { moth *Talker } func (p *Person) speak() string { return (*p.moth).talk() } func TestPerson(t…
Amar Prakash Pandey
  • 1,278
  • 18
  • 23
0
votes
0 answers

Gomock not working for sending data to cockroach DB

I am writing a unit test for a function which calls a goroutine to send data to cockroach db. I am trying to mock the call to cockroach db which doesn't seem to work.The test case results in time out in the exact same manner as it would have if…
0
votes
1 answer

How to Mock inner methods in GoLang

e.g type test struct { // few fields} func (t *test) createresource(res1 string,res2 string)error { //doing some task t.createsubresource(res1) } func (t *test)createsubresource(res1 string)error{ //perform some task } I want to…
0
votes
1 answer

Is there a way to mock a type assertion using gomock?

i'm new to golang and using Gomock for testing. I have generated the mock for the interface foo, but in my code theres a piece of logic that uses foo.(type). May I know if theres a way to mock this and return a type of my choice? If not, what would…
kito
  • 41
  • 7
0
votes
1 answer

How to write Unit test in Golang usng echo for end point url using go mock-gen mocking?

I write this go code for login. Now i want to unit test my code. This code is depends on controller to service layer then service to repository layer. I want to use gomock tool for mocking, if any other please suggest me. I'm using echo…
Md. Abu Farhad
  • 373
  • 5
  • 21
0
votes
1 answer

Adding gomock tests to project gives "build constraints exclude all Go files" when running test

I add a test file for a package using gomock to an existing Go project, and now I am getting ...imports github.com/golang/mock: build constraints exclude all Go files in /home/bserdar/go/pkg/mod/github.com/golang/mock@v1.4.3 when I run go test…
Burak Serdar
  • 46,455
  • 3
  • 40
  • 59
0
votes
1 answer

How to compare/match closures in mocks?

TL;DR: mocked method accepts closure. I wonder how to create custom matcher (https://godoc.org/github.com/golang/mock/gomock#Matcher): closure itself in turn is working with private structure - meaning I can't even call the closure in my test to…
Nikita
  • 31
  • 5
0
votes
2 answers

Is it possible to assert that a real method is called via spying in Go like Java Mockito?

I am looking for asserting that a statement is covered in my test. For instance, let's say from test am calling methodA() which has reference to methodB(). I would like to assert that methodB() is called upon executing methodA() from tests. In the…
Mozhi
  • 757
  • 1
  • 11
  • 28
0
votes
1 answer

How do you override calls being made in GoMock?

I'm trying to write unit tests mocking service calls being made using GoMocks and It's been a real struggle. One way I'm trying to simplify the code is to make a private method that gives a good response for all the mocks and then in each individual…
ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
0
votes
1 answer

How to deal with random input and output values

I have a wrapper interface that defines a Downloadfile function. The problem is that one of parameters has a random temporary directoriename and the output is a random temporary filename. How do I mock it using https://github.com/golang/mock so I…
Gert Cuykens
  • 6,845
  • 13
  • 50
  • 84