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
18
votes
3 answers

Find version of a module

We are working with Go modules. I want in a CLI to get the specific version of a module. Is it possible? If you are curious, the reason is that I want to add the following generate command: //go:generate go run github.com/golang/mock/mockgen…
ElyashivLavi
  • 1,681
  • 3
  • 22
  • 33
12
votes
1 answer

Testing with Gomock returns error: Expected call has already been called the max number of times

I am using Gomock https://godoc.org/github.com/golang/mock and mockgen The Source code for this test is: package sqs import ( "fmt" "log" "os" "runtime" "github.com/aws/aws-sdk-go/aws/session" …
Alwin Doss
  • 962
  • 2
  • 16
  • 33
9
votes
1 answer

resolving imported package collision for mockgen

I have a package collision where I get the following error: mockgen -package=mocks -source=myproto.pb.go -destination=mocks/my_mocks.go imported package collision: "foo" imported twice I see one import that is obvious: import foo…
Bren
  • 3,516
  • 11
  • 41
  • 73
8
votes
2 answers

Which directory to put mocks?

I've been most recently experimenting with GoMock, the test mocking framework supported by the official creators of the Go language. I was wondering where is the most reasonable place to put these mocked files. My current directory structure is as…
hlin117
  • 20,764
  • 31
  • 72
  • 93
7
votes
1 answer

Testing and mocking a function which does not return any value

I want to test a function, which does not return any value, but instead triggers other functions. While reading about testing, I found information that this is called a behavioral verification and that by mocking I can check what functions and in…
Ziva
  • 3,181
  • 15
  • 48
  • 80
7
votes
3 answers

how to inject an url to httptest.server in golang?

For sentence resp, err := client.Get(fmt.Sprintf("https://www.xxxxx/day?time=%s", time)) If I want to mock a response to this client.Get() in unit test, I should use httptest.server, but how can I bind the url (https://www.xxxxx/day?time=%s) to the…
Pineapple
  • 111
  • 1
  • 4
7
votes
2 answers

Golang: how to mock ...interface{} arguents using gomock

I have a Printer interface that uses the standard go Printf function signature: type Printer interface { Printf(format string, tokens ...interface{}) } I would like to be able to mock this interface using gomock, but I'm not sure how setup the…
Zack
  • 71
  • 1
  • 5
6
votes
4 answers

zsh: command not found: mockgen - golang 1.18 - macOS Monterrey

I'm new to go. Currently I'm using zsh terminal in macOS, just followed the instructions pointed out here https://github.com/golang/mock when installing go mock. However when trying to execute a mockgen command I keep seeing zsh: command not found:…
5
votes
1 answer

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

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…
Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88
4
votes
1 answer

Optional calls in GoMock

I want to use GoMock to test some code, without coupling my tests too tightly to the actual implementation of the thing under test. But GoMock seems to require that I be able to say in advance exactly what calls the code under test will make, even…
interfect
  • 2,665
  • 1
  • 20
  • 35
4
votes
0 answers

How to use gomock with sqlc

I'd like to write unit tests for functions generated by sqlc but I don't understand how to use gomock. I'm new to mocking. Below I'll describe a bit how sqlc generation works. So, sqlc generates an interface DBTX: type DBTX interface { …
4
votes
3 answers

Can I mock a function with pointer parameter which need to be used

Let's say we have a library provide a function Double to double the integer, we use pointer i to get the result value not by return: package api type Action interface { Double(i *int) error } type NUM struct{} func (n NUM) Double(i *int)…
hotsnow
  • 59
  • 1
  • 1
  • 3
4
votes
1 answer

Using testing.T in TestMain

There are a few test cases I want to run for which there is a need to start a GRPC mock server. I am using gomock library for this. To start the server, I have to pass a variable of type testing.T to this function - gomock.NewController(). Since…
aniztar
  • 2,443
  • 4
  • 18
  • 24
4
votes
1 answer

How to run mockgen during build?

I have started using gomock to create mock objects for unit testing. Gomock requires that that I run the mockgen command with certain argument in order to generate code for the mock. This needs to be done again every time the interfaces that is…
Vebjorn Ljosa
  • 17,438
  • 13
  • 70
  • 88
3
votes
1 answer

Testing my interface in golang with mocks. Specifically test 1 function that calls a sibling function

Let's say I have this code and I want to create a test for Foo() The important part it Foo makes a call to Bar package main type MyInterface interface { Foo() error Bar() error } type MyStruct struct { } func NewMyStruct() MyInterface{ …
Mexicoder
  • 517
  • 1
  • 6
  • 18
1
2 3 4 5