Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/coc1961/gomock
https://github.com/coc1961/gomock
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/coc1961/gomock
- Owner: coc1961
- Created: 2020-06-02T23:34:35.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-09-18T20:26:33.000Z (over 2 years ago)
- Last Synced: 2024-06-20T05:09:19.182Z (6 months ago)
- Language: Go
- Size: 21.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gomock
> This program creates mocks based on Go interface
> for example
> Running
```sh
gomock -s $GOROOT/src/io/io.go -n ReadCloser
```
> The Mock Created Is
```go
// Interface compatible with ReadCloser that contains
// the Mock function to access the Mock instance
type ReadCloserMockInterface interface {
io.ReadCloser
Mock() *ReadCloserMock
}// function to create the mock
func NewReadCloserMock() ReadCloserMockInterface {
return &ReadCloserMock{}
}// function to access the mock instance
// for example
// var myVar ReadCloser
// mock := NewReadCloserMock()
// mock.Mock().Callbackxxx = func(...)...{} // Modifies the default behavior of the mock function
// myVar = mock // Compatible interface!!
func (m *ReadCloserMock) Mock() *ReadCloserMock {
return m
}// Mock for ReadCloser interface
type ReadCloserMock struct {
CallbackRead func(p []byte) (n int, err error)
CallbackClose func() (retVar0 error)
}// Read function
func (m *ReadCloserMock) Read(p []byte) (n int, err error) {
if m.CallbackRead != nil {
return m.CallbackRead(p)
}
return n, err
}// Close function
func (m *ReadCloserMock) Close() (retVar0 error) {
if m.CallbackClose != nil {
return m.CallbackClose()
}
return retVar0
}```