Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Automated tool for mocking go interfaces. Works with go:generate.

License

NotificationsYou must be signed in to change notification settings

nicheinc/mock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock is a code generation tool meant to be used withgo generate. Itgenerates simple mock implementations of interfaces for use in testing.

Mocks are thread-safe.

Installation

You can installmock locally using...

go install github.com/nicheinc/mock

...or just usego:generate go run github.com/nicheinc/mock@main tohave thego generate command automatically use the latest version (see belowfor example usage).

Usage

The only required argument tomock is the name of the interface to mock,which must be provided after all other flags:

Usage: mock [options] interfaceOptions:  -d string    Directory to search for interface in (default ".")  -o string    Output file (default stdout)

Example

Given this interface:

package maintypeGetterinterface {GetByID(idint) ([]string,error)GetByName(namestring) ([]string,error)}

mock Getter will generate an implementation like this, and print it tostdout:

package main// GetterMock is a mock implementation of the Getter// interface.typeGetterMockstruct {GetByIDStubfunc(idint) ([]string,error)GetByIDCalledint32GetByNameStubfunc(namestring) ([]string,error)GetByNameCalledint32}var_Getter=&GetterMock{}// GetByID is a stub for the Getter.GetByID// method that records the number of times it has been called.func (m*GetterMock)GetByID(idint) ([]string,error) {atomic.AddInt32(m.GetByIDCalled,1)returnm.GetByIDStub(id)}// GetByName is a stub for the Getter.GetByName// method that records the number of times it has been called.func (m*GetterMock)GetByName(namestring) ([]string,error) {atomic.AddInt32(m.GetByNameCalled,1)returnm.GetByNameStub(name)}

Go Generate

To use withgo generate, simply place ago:generate comment somewhere inyour package (e.g. above the interface definition), like so:

//go:generate go run github.com/nicheinc/mock@main -o getter_mock.go Getter

Note the use of the-o flag, which specifies the output file. If this flagis not provided, the mocked implementation will be printed to stdout.

Then run thego generate command from the package directory.

Voila! There should now be agetter_mock.go file containing your new mock, inthe same package as the interface definition. Subsequent runs ofgo generatewill overwrite the file, so be careful not to edit it!

About

Automated tool for mocking go interfaces. Works with go:generate.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go100.0%

[8]ページ先頭

©2009-2025 Movatter.jp