Home > Backend Development > Golang > mockgen does not create mocks

mockgen does not create mocks

WBOY
Release: 2024-02-06 08:45:03
forward
1015 people have browsed it

mockgen 不创建模拟

Question content

This is my folder structure:

demo/
|-- go.mod
|-- main.go
|-- interfaces/
|   |-- interfaces.go
|-- mocks/
Copy after login

This is my go.mod file

go 1.20

require (
    github.com/golang/mock v1.6.0 // indirect
    golang.org/x/mod v0.4.2 // indirect
    golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
    golang.org/x/tools v0.1.1 // indirect
    golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
Copy after login

This is my interfaces.go

package interfaces

type MyInterface interface {
    DoSomething() error
    GetValue() int
}
Copy after login

When I try to run the mockgen command, instead of creating the mock, it gives me this help description: mockgen has two operating modes: source and reflect. Source mode generates mock interfaces from source files. It is enabled by using the -source flag. Other flags that may be useful in this mode are -imports and -aux_files.

https://manpages.org/mockgen

I ran the following command: mockgen -destination=mocks/mock_myinterface.go -package=mocks demo/interfaces MyInterface

How to use mockgen command correctly?


Correct answer


You should probably use the fork maintained by Uber (because Google abandoned golang/mock): https: //github.com/uber -go/mock

If you just run the mockgen command you will get the command options. Something that should work for you is this:

mockgen -source interfaces/interfaces.go -destination mocks/mocks.go
Copy after login

The above is the detailed content of mockgen does not create mocks. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template