### Install mockgen for Go versions < 1.16 Source: https://github.com/golang/mock/blob/main/README.md Installs the latest released version of the mockgen tool using `go get` for Go versions prior to 1.16. Ensure `$GOPATH/bin` is in your PATH. ```bash GO111MODULE=on go get github.com/golang/mock/mockgen@v1.6.0 ``` -------------------------------- ### Install mockgen for Go 1.16+ Source: https://github.com/golang/mock/blob/main/README.md Installs the latest released version of the mockgen tool using `go install` for Go versions 1.16 and later. It is recommended to fixate on a specific mockgen version in CI pipelines. ```bash go install github.com/golang/mock/mockgen@v1.6.0 ``` -------------------------------- ### Generate mocks using reflection mode with package path Source: https://github.com/golang/mock/blob/main/README.md Generates mock interfaces by building a Go program that uses reflection. This example specifies the package path 'database/sql/driver' and the interfaces 'Conn' and 'Driver' to mock. ```bash mockgen database/sql/driver Conn,Driver ``` -------------------------------- ### Build Mock Object in Go with gomock Source: https://github.com/golang/mock/blob/main/README.md This snippet demonstrates how to create a mock object for a given interface using gomock. It sets up expectations for a method call and verifies its execution. ```go type Foo interface { Bar(x int) int } func SUT(f Foo) { // ... } func TestFoo(t *testing.T) { ctrl := gomock.NewController(t) // Assert that Bar() is invoked. defer ctrl.Finish() m := NewMockFoo(ctrl) // Asserts that the first and only call to Bar() is passed 99. // Anything else will fail. m. EXPECT(). Bar(gomock.Eq(99)). Return(101) SUT(m) } ``` -------------------------------- ### Generate mocks from source file using mockgen Source: https://github.com/golang/mock/blob/main/README.md Generates mock interfaces from a Go source file using mockgen's source mode. The `-source` flag specifies the input file, and other options can refine the generation process. ```bash mockgen -source=foo.go [other options] ``` -------------------------------- ### Generate mocks using reflection mode with current package Source: https://github.com/golang/mock/blob/main/README.md Generates mock interfaces using reflection mode for the current package (represented by '.'). This is convenient for use with `go:generate` directives. ```bash mockgen . Conn,Driver ``` -------------------------------- ### Go Bash Commands for Mock Generation and Testing Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/mock_in_test_package/README.md These bash commands demonstrate the process of generating mocks and running tests in a Go project. It highlights the failure before a patch and success after the patch is applied, indicating the resolution of build errors related to undefined types. ```bash $ go generate $ go test # github.com/golang/mock/mockgen/internal/tests/mock_in_test_package_test [github.com/golang/mock/mockgen/internal/tests/mock_in_test_package.test] ./mock_test.go:36:44: undefined: User ./mock_test.go:38:21: undefined: User FAIL github.com/golang/mock/mockgen/internal/tests/mock_in_test_package [build failed] ``` ```bash $ go generate $ go test ok github.com/golang/mock/mockgen/internal/tests/mock_in_test_package 0.031s ``` -------------------------------- ### Build Stub Object in Go with gomock Source: https://github.com/golang/mock/blob/main/README.md This snippet shows how to create a stub object using gomock, which allows defining behavior for method calls without making assertions on the call itself. Useful for isolating dependencies. ```go type Foo interface { Bar(x int) int } func SUT(f Foo) { // ... } func TestFoo(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() m := NewMockFoo(ctrl) // Does not make any assertions. Executes the anonymous functions and returns // its result when Bar is invoked with 99. m. EXPECT(). Bar(gomock.Eq(99)). DoAndReturn(func(_ int) int { time.Sleep(1*time.Second) return 101 }). AnyTimes() // Does not make any assertions. Returns 103 when Bar is invoked with 101. m. EXPECT(). Bar(gomock.Eq(101)). Return(103). AnyTimes() SUT(m) } ``` -------------------------------- ### Bash: Mockgen Generation Success (After Patch) Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/custom_package_name/README.md This snippet shows the successful execution of `go generate` with mockgen after the patch. It confirms that mockgen can now automatically resolve custom package names, resulting in a zero exit status. ```bash $ go generate greeter/greeter.go $ echo $? ``` -------------------------------- ### Bash: Mockgen Generation Failure (Before Patch) Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/custom_package_name/README.md This snippet demonstrates the failure of `go generate` with mockgen before a patch that addresses custom package name resolution. It shows the error message indicating an unknown package, highlighting the problem mockgen faced. ```bash $ go generate greeter/greeter.go 2018/03/05 22:44:52 Loading input failed: greeter.go:17:11: failed parsing returns: greeter.go:17:14: unknown package "client" greeter/greeter.go:1: running "mockgen": exit status 1 ``` -------------------------------- ### Modify 'Got' Failure Message in Go with gomock Source: https://github.com/golang/mock/blob/main/README.md This Go snippet demonstrates how to customize the 'Got' part of a failure message in gomock. It uses gomock.GotFormatterAdapter to change how received values are formatted, especially for types like byte slices. ```go gomock.GotFormatterAdapter( gomock.GotFormatterFunc(func(i interface{}) string { // Leading 0s return fmt.Sprintf("%02d", i) }), gomock.Eq(15), ) ``` -------------------------------- ### Modify 'Want' Failure Message in Go with gomock Source: https://github.com/golang/mock/blob/main/README.md This Go code snippet illustrates how to customize the 'Want' part of a failure message generated by gomock matchers. It uses gomock.WantFormatter to change the default string representation. ```go gomock.WantFormatter( gomock.StringerFunc(func() string { return "is equal to fifteen" }), gomock.Eq(15), ) ``` -------------------------------- ### Go: Source Interface with Aliased Import Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/aux_imports_embedded_interface/README.md This Go code snippet demonstrates the structure of a source interface that embeds another interface from an aliased imported package. This pattern can trigger 'unknown embedded interface' errors during mock generation if not handled correctly by the parsing logic. ```go // source import ( alias "some.org/package/imported" ) type Source interface { alias.Foreign } ``` -------------------------------- ### Golang Interface Definition with Conflicting Names Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/generated_identifier_conflict/README.md This Go code snippet demonstrates an interface definition where method parameters share names with potential generated receiver names, illustrating a common source of identifier conflicts in mock generation. This can lead to compilation failures if not handled properly. ```go type Example interface { Method(_m, _mr, m, mr int) } ``` -------------------------------- ### Go: Mock Generation Error Handling for Embedded Interfaces Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/aux_imports_embedded_interface/README.md This Go code snippet illustrates a critical part of the mock generation parsing logic where an assumption is made about embedded interfaces always residing within the current package. This assumption leads to 'unknown embedded interface' errors when the embedded interface is external or aliased. ```go case *ast.Ident: // Embedded interface in this package. ei := p.auxInterfaces[""][v.String()] if ei == nil { return nil, p.errorf(v.Pos(), "unknown embedded interface %s", v.String()) } ``` -------------------------------- ### Go: Imported Foreign Interface with Embedded Interface Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/aux_imports_embedded_interface/README.md This Go code defines a 'Foreign' interface that embeds an 'Embedded' interface. This is the structure that causes issues when the 'Foreign' interface is imported and embedded within another source interface, leading to mock generation failures. ```go // some.org/package/imported type Foreign interface { Embedded } type Embedded interface {} ``` -------------------------------- ### Golang Generated Mock Method with Conflicting Names Source: https://github.com/golang/mock/blob/main/mockgen/internal/tests/generated_identifier_conflict/README.md This Go code snippet shows a generated mock method where the receiver name (`_m`) conflicts with a parameter name (`_m`) from the original interface definition. This conflict prevents the mock code from compiling successfully. The generated code needs to ensure unique identifiers. ```go // Method mocks base method func (_m *MockExample) Method(_m int, _mr int, m int, mr int) { _m.ctrl.Call(_m, "Method", _m, _mr, m, mr) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.