Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/thomaslanghorst/mock-generator

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/thomaslanghorst/mock-generator

  • v0.0.0-20211114085523-9fc143195ce9
  • Source
  • Go
  • Socket score

Version published
Created
Source

mock-generator

This is a small project I wrote to help create mock files using golangs template package. It will generate mock files that would be written using the mock package of testify.

Build the binary

To build the mock-generator you simply have to build the binary using

go build -o ./bin/mock-generator

If you want to make it gobally available, just export the path to your ~/.bashrc file.

export PATH=$PATH:</path/to/your/project>/bin/mock-generator

Running the mock-generator

After you have build the binary you can call and test it using the ./example/service.go file.

./bin/mock-generator -i ./example/service.go

This will generate a file called service_mock.go next to the service.go file.

Example

If you take this simple example

package service

type User struct {
}

type ServiceInterface interface {
	Login(userId string, password string) error
	ListUsers() ([]User, error)
	Logout(userId string)
}

and run the mock-generator. The output file will look like this:

package service

import "github.com/stretchr/testify/mock"

type MockServiceInterface struct {
	mock.Mock
}

func (m *MockServiceInterface) Login(userId string, password string) error {
    args := m.Called(userId, password)
	
	return args.Error(0)
}

func (m *MockServiceInterface) ListUsers() ([]User, error) {
    args := m.Called()
	
	var v0 []User
	if args.Get(0) != nil {
		v0 = args.Get(0).([]User)
	}
	return v0, args.Error(1)
}

func (m *MockServiceInterface) Logout(userId string)  {
    m.Called(userId)
	
	
}

FAQs

Package last updated on 14 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc